UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212770#3828. Bchendongyu062ms2868kbC++112.3kb2024-10-20 10:47:562024-10-20 14:37:50

answer

#include<bits/stdc++.h>
using namespace std;
int n,m,ansx,ansy;
int vis[400500],a[400500];
stack<int> ans;
int move_[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
	int x,y;
}fa[200500];
pair<int,node> t;
vector<pair<int,node>> first_q;
queue<pair<int,node>> q;
int two_to_one(int a,int b)
{
	return (a-1)*m+b;
}
bool cmp(pair<int,node> A,pair<int,node> B)
{
	return a[two_to_one(A.second.x,A.second.y)]<a[two_to_one(B.second.x,B.second.y)];
}
void bfs()
{
	first_q.clear();
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			first_q.push_back(make_pair(1,(node){i,j}));
		}
	}
	sort(first_q.begin(),first_q.end(),cmp);
	for(pair<int,node> i:first_q)
	{
		q.push(i);
	}
	while(!q.empty())
	{
		t=q.front();
		q.pop();
		if(t.first>vis[two_to_one(ansx,ansy)])
		{
			ansx=t.second.x,ansy=t.second.y;
		}
		if(t.first==vis[two_to_one(ansx,ansy)])
		{
			if(a[two_to_one(t.second.x,t.second.y)]<a[two_to_one(ansx,ansy)])
			{
				ansx=t.second.x,ansy=t.second.y;
			}
		}
		for(int i=0;i<4;i++)
		{
			int tx=t.second.x+move_[i][0];
			int ty=t.second.y+move_[i][1];
			if(tx&&ty&&tx<=n&&ty<=m)
			{
				if(a[two_to_one(tx,ty)]>a[two_to_one(t.second.x,t.second.y)])
				{
					if(vis[two_to_one(tx,ty)]<t.first+1)
					{
						fa[two_to_one(tx,ty)]={t.second.x,t.second.y};
						vis[two_to_one(tx,ty)]=t.first+1;
						q.push(make_pair(t.first+1,(node){tx,ty}));
					}
					else if(vis[two_to_one(tx,ty)]==t.first+1&&a[two_to_one(fa[two_to_one(tx,ty)].x,fa[two_to_one(tx,ty)].y)]>a[two_to_one(t.second.x,t.second.y)])
					{
						fa[two_to_one(tx,ty)]={t.second.x,t.second.y};
					}
				}
			}
		}
	}
}
void work()
{
	ansx=1,ansy=0;
	scanf("%d%d",&n,&m);
	memset(a,-0x3f,sizeof a);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			scanf("%d",a+two_to_one(i,j));
			vis[two_to_one(i,j)]=1;
			fa[two_to_one(i,j)]=node{i,j};
		}
	}
	bfs();
	printf("%d\n",vis[two_to_one(ansx,ansy)]);
	do
	{
		ans.push(a[two_to_one(ansx,ansy)]);
		int o=two_to_one(ansx,ansy);
		ansx=fa[o].x;
		ansy=fa[o].y;
	}while(fa[two_to_one(ansx,ansy)].x!=ansx||fa[two_to_one(ansx,ansy)].y!=ansy);
	ans.push(a[two_to_one(ansx,ansy)]);
	while(!ans.empty())
	{
		printf("%d ",ans.top());
		ans.pop();
	}
	printf("\n");
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		work();
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 2820kb

input:

10
3 3
0 1 2
3 4 5
6 7 8
3 3
0 1 2
3 4 5
6 8 7
3 3
0 1 2
3 4 5
7 6 8
3 3
0 1 2
3 4 5
7 8 6
3 3
0 1 2...

output:

5
0 1 2 5 8 
6
0 1 2 5 7 8 
5
0 1 4 6 7 
6
0 1 2 5 6 8 
5
0 1 2 5 7 
7
0 1 2 5 6 7 8 
5
0 1 2 6 8 
6...

result:

wrong answer 17th numbers differ - expected: '2', found: '4'

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 60ms
memory: 2868kb

input:

10
22 45
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33...

output:

990
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3...

result:

wrong answer 1003rd numbers differ - expected: '11', found: '114'

Subtask #3:

score: 0
Time Limit Exceeded

Test #5:

score: 0
Time Limit Exceeded

input:

10
2 100000
143604 106821 145034 44402 118718 156663 77133 28800 81890 12336 191537 118894 103331 75...

output:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #6:

score: 0
Time Limit Exceeded

input:

10
1 200000
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32...

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #7:

score: 0
Time Limit Exceeded

input:

10
145 1379
140324 86968 96426 123781 39754 103720 60835 118904 114639 53717 27146 110309 39232 5608...

output:


result: