#include <bits/stdc++.h>
using namespace std;
int n,M,Q;
unordered_map <int,int> v;
int stanga(int x,int y)
{
int cnt=0;
while(x!=y)
{
cnt++;
x--;
if(x<0)
x=2*n-1;
}
return cnt;
}
int dreapta(int x,int y)
{
int cnt=0;
while(x!=y)
{
cnt++;
x++;
if(x>=2*n)
x=0;
}
return cnt;
}
int shc(int x, int y)
{
int pas1=0;
int pas4=0;
int pas3=0;
int pas2=0;
while(x!=y)
{
if(v[x]!=0)
{
pas1=stanga(v[x],y)+1;
pas2=dreapta(v[x],y)+1;
break;
}
++pas1;
++pas2;
x--;
if(x<0)
x=2*n-1;
}
pas1=min(pas1,pas2);
while(x!=y)
{
if(v[x]!=0)
{
pas3=stanga(v[x],y)+1;
pas4=dreapta(v[x],y)+1;
break;
}
pas3++;
++pas4;
x++;
if(x>=2*n)
x=0;
}
pas2=min(pas3,pas4);
return min(pas1,pas2);
}
int main()
{
cin>>n>>M>>Q;
for(int i=1;i<=M;++i)
{
int x;
cin>>x;
if(x>=n)
{
v[x-n]=x;
v[x]=x-n;
}
if(x<n)
{
v[x+n]=x;
v[x]=x+n;
}
}
for(int i=1;i<=Q;++i)
{
int a,b;
cin>>a>>b;
cout<<min(shc(a,b),min(stanga(a,b),dreapta(a,b)))<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
336 KB |
Output is correct |
2 |
Correct |
36 ms |
336 KB |
Output is correct |
3 |
Correct |
47 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Execution timed out |
2095 ms |
452 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
6 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
6 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2049 ms |
336 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
336 KB |
Output is correct |
2 |
Correct |
36 ms |
336 KB |
Output is correct |
3 |
Correct |
47 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Execution timed out |
2095 ms |
452 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |