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>
#ifndef SKY
#include "rail.h"
#endif // SKY
using namespace std;
#define N 5010
#define ll long long
#define fs first
#define sc second
#define ii pair<int,int>
#define pb push_back
#define iii pair<int,pair<int,int>>
int dis[N][N],n,location[N],stype[N];
#ifdef SKY
int pos[N],type[N];
int getDistance(int u,int v)
{
if(pos[u]>pos[v])
swap(u,v);
if(u==v)
return 0;
int L=-1,R=-1;
for(int i=0;i<n;i++)
if(type[i]==0&&pos[i]<=pos[u]&&(L==-1||pos[i]>pos[L]))
L=i;
for(int i=0;i<n;i++)
if(type[i]==1&&pos[i]>=pos[v]&&(R==-1||pos[i]<pos[R]))
R=i;
return pos[R]-pos[L]+(pos[u]-pos[L])+(pos[R]-pos[v]);
}
#endif // SKY
int my_ask(int u,int v)
{
if(u==v)
return 0;
if(dis[u][v]!=-1)
return dis[u][v];
dis[u][v]=dis[v][u]=getDistance(u,v);
return dis[u][v];
}
void solve(vector<int>s,int moc,int val)
{
while(!s.empty())
{
vector<int>luu;
int u=-1;
for(auto i:s)
if(u==-1||my_ask(i,moc)<my_ask(u,moc))
u=i;
if(val==0)
location[u]=location[moc]-my_ask(u,moc);
else location[u]=location[moc]+my_ask(u,moc);
stype[u]=val;
for(auto i:s)
if(i!=u)
{
if(my_ask(moc,i)==my_ask(moc,u)+my_ask(u,i))
{
location[i]=(val==0 ? location[u]+my_ask(u,i) : location[u]-my_ask(u,i));
stype[i]=(val^1);
}else
{
luu.pb(i);
}
}
s=luu;
}
}
void findLocation(int cc, int first, int location_ans[], int stype_ans[])
{
n=cc;
memset(dis,-1,sizeof(dis));
location[0]=first;
stype[0]=0;
int R=-1;
for(int i=1;i<n;i++)
if(R==-1||my_ask(i,0)<my_ask(R,0))
R=i;
location[R]=location[0]+my_ask(R,0);
stype[R]=1;
int L=-1;
for(int i=0;i<n;i++)
if(i!=R&&(L==-1||my_ask(i,R)<my_ask(L,R)))
L=i;
location[L]=location[R]-my_ask(L,R);
stype[L]=0;
//cout<<L<<" "<<R<<endl;
vector<int>lis_left,list_right;
for(int i=0;i<n;i++)
if(i!=L&&i!=R)
{
if(my_ask(i,L)>my_ask(i,R))
lis_left.pb(i);
else list_right.pb(i);
}
solve(lis_left,R,0);
solve(list_right,L,1);
for(int i=0;i<n;i++)
{
stype[i]++;
location_ans[i]=location[i];
stype_ans[i]=stype[i];
}
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
assert(location[i]!=location[j]);
#ifdef SKY
for(int i=0;i<n;i++)
cout<<location[i]<<" "<<stype[i]<<endl;
#endif // SKY
}
#ifdef SKY
int main()
{
freopen("A.inp","r",stdin);
freopen("A.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>pos[i]>>type[i];
int location[n],stype[n];
findLocation(n,pos[0],location,stype);
return 0;
}
#endif
# | 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... |