이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 wibu_never_die,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];
}
bool SS(const int&u,const int&v)
{
return my_ask(u,wibu_never_die)<my_ask(v,wibu_never_die);
}
void solve(vector<int>s,int moc,int val)
{
if(s.empty())
return;
wibu_never_die=moc;
sort(s.begin(),s.end(),SS);
vector<int>lis;
for(auto i:s)
{
if(i==s[0])
{
lis.pb(i);
stype[i]=val;
location[i]=(val==0 ? location[moc]-my_ask(i,moc) : location[moc]+my_ask(i,moc));
continue;
}
if(val==0)
{
int length=my_ask(lis.back(),moc)-my_ask(lis.back(),i),vt=-1;
for(int j=lis.size()-1;j>=0;j--)
if(vt==-1||my_ask(moc,lis[j])>=length)
vt=lis[j];
length=my_ask(moc,vt)-length;
if(my_ask(moc,i)==length+my_ask(moc,vt))
{
stype[i]=(val^1);
location[i]=location[vt]+length;
}else
{
lis.pb(i);
stype[i]=val;
location[i]=(val==0 ? location[moc]-my_ask(i,moc) : location[moc]+my_ask(i,moc));
}
}else
{
int length=my_ask(lis.back(),moc)-my_ask(lis.back(),i),vt=-1;
for(int j=lis.size()-1;j>=0;j--)
if(vt==-1||my_ask(moc,lis[j])>=length)
vt=lis[j];
length=my_ask(moc,vt)-length;
if(my_ask(moc,i)==length+my_ask(moc,vt))
{
stype[i]=(val^1);
location[i]=location[vt]-length;
}else
{
lis.pb(i);
stype[i]=val;
location[i]=(val==0 ? location[moc]-my_ask(i,moc) : location[moc]+my_ask(i,moc));
}
}
}
}
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=0;
vector<int>lis_left,list_right;
for(int i=0;i<n;i++)
if(i!=L&&i!=R)
{
if(my_ask(L,i)==my_ask(L,R)+my_ask(i,R)&&my_ask(i,R)<=my_ask(L,R))
{
location[i]=location[R]-my_ask(i,R);
stype[i]=0;
continue;
}
if(my_ask(i,L)-my_ask(L,R)==my_ask(i,R))
lis_left.pb(i);//,cout<<i<<endl;
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];
}
#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... |