제출 #719148

#제출 시각아이디문제언어결과실행 시간메모리
719148bin9638철로 (IOI14_rail)C++17
100 / 100
117 ms98900 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...