This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |