#include <bits/stdc++.h>
using namespace std;
int n,M,Q;
int v[1000001];
int stanga(int x,int y)
{
if(x<y)
return 2*n-y+x;
else
return x-y;
}
int dreapta(int x,int y)
{
if(x>y)
return 2*n-x+y;
else
return y-x;
}
int shc(int x, int y)
{
int pas1=1;
int pas4=1;
int pas3=1;
int pas2=1;
int ok=0;
int k=x-1;
int t;
if(k<0)
k=2*n-1;
for(int i=1;i<=M;++i)
if(v[i]==k)
ok=1;
if(ok)
{
if(k>=n)
{
t=k-n;
pas1+=stanga(k,y)+1;
pas2+=dreapta(k,y)+1;
}
else
{
t=k+n;
pas1+=stanga(t,y)+1;
pas2+=dreapta(t,y)+1;
}
pas1=min(pas1,pas2);
}
else
pas1=INT_MAX;
k=x+1;
if(k>=2*n)
k=0;
ok=0;
for(int i=1;i<=M;++i)
if(v[i]==k)
ok=1;
if(ok)
{
if(k>=n)
{
t=k-n;
pas3+=stanga(k,y)+1;
pas4+=dreapta(k,y)+1;
}
else
{
t=k+n;
pas3+=stanga(t,y)+1;
pas4+=dreapta(t,y)+1;
}
pas2=min(pas3,pas4);
}
else
pas2=INT_MAX;
pas3=1;
pas4=1;
for(int i=1;i<=M;++i)
if(v[i]==x)
ok=1;
if(ok)
{
if(x>=n)
{
t=x-n;
pas3+=stanga(x,y);
pas4+=dreapta(x,y);
}
else
{
t=x+n;
pas3+=stanga(t,y);
pas4+=dreapta(t,y);
}
pas3=min(pas3,pas4);
}
else
pas3=INT_MAX;
return min(pas1,min(pas2,pas3));
}
int main()
{
cin>>n>>M>>Q;
for(int i=1;i<=M;++i)
{
cin>>v[i];
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
336 KB |
Output is correct |
2 |
Correct |
34 ms |
452 KB |
Output is correct |
3 |
Correct |
34 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
42 ms |
480 KB |
Output is correct |
6 |
Correct |
37 ms |
840 KB |
Output is correct |
7 |
Correct |
53 ms |
888 KB |
Output is correct |
8 |
Correct |
42 ms |
848 KB |
Output is correct |
9 |
Correct |
41 ms |
928 KB |
Output is correct |
10 |
Correct |
42 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
3 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
3 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
840 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Execution timed out |
2066 ms |
6376 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
336 KB |
Output is correct |
2 |
Correct |
34 ms |
452 KB |
Output is correct |
3 |
Correct |
34 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
42 ms |
480 KB |
Output is correct |
6 |
Correct |
37 ms |
840 KB |
Output is correct |
7 |
Correct |
53 ms |
888 KB |
Output is correct |
8 |
Correct |
42 ms |
848 KB |
Output is correct |
9 |
Correct |
41 ms |
928 KB |
Output is correct |
10 |
Correct |
42 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Incorrect |
3 ms |
336 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |