#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <queue>
#include <string.h>
int n,m,q,v[50001];
std::pair<int,int> per[50001];
int cautare(int a,int b)
{
int cnt=0;
std::queue<std::pair<int,int>> co;
co.push({a,0});
while(!co.empty())
{
int x=co.front().first;
int t=co.front().second;
if(x==b)
return t;
int st=x-1,dr=x+1;
if(st<0)
st=2*n-1;
if(dr>n*2-1)
dr=0;
if(x==b)
return cnt;
else
cnt++;
co.pop();
co.push({st,t+1});
co.push({dr,t+1});
if(v[x]!=-1)
co.push({v[x],t+1});
}
}
int main()
{
std::cin>>n>>m>>q;
for(int i=0;i<=2*n-1;++i)
v[i]=-1;
for(int i=1;i<=m;++i)
{
int x;
std::cin>>x;
if(x>=n)
{
v[x]=x-n;
v[x-n]=x;
}
else
{
v[x]=x+n;
v[x+n]=x;
}
}
for(int i=1;i<=q;++i)
std::cin>>per[i].first>>per[i].second;
for(int i=1;i<=q;++i)
{
int x1=per[i].first,y1=per[i].second;
std::cout<<cautare(x1,y1)<<'\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int cautare(int, int)':
Main.cpp:11:36: warning: control reaches end of non-void function [-Wreturn-type]
11 | std::queue<std::pair<int,int>> co;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
600 KB |
Output is correct |
2 |
Correct |
19 ms |
604 KB |
Output is correct |
3 |
Execution timed out |
2131 ms |
1000776 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2072 ms |
1014864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2072 ms |
1014864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
856 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
600 KB |
Output is correct |
2 |
Correct |
19 ms |
604 KB |
Output is correct |
3 |
Execution timed out |
2131 ms |
1000776 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |