제출 #617061

#제출 시각아이디문제언어결과실행 시간메모리
617061rrrr10000Shortcut (IOI16_shortcut)C++14
71 / 100
2081 ms2900 KiB
#include "shortcut.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<bool> vb; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++) #define all(a) a.begin(),a.end() #define fi first #define se second #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} #define pb emplace_back template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<endl;} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;} template<class T> void outvv(T v){for(auto x:v)outv(x);} const ll inf=1001001001001001001; const ll INF=1001001001; long long find_shortcut1(int n, std::vector<int> l_, std::vector<int> d_, int c){ vi d(n),v(n); rep(i,n-1)d[i+1]=d[i]+l_[i]; rep(i,n)v[i]=d_[i]; vi memol(n),memor(n); rep(l,n)REP(r,l+1,n){ chmax(memol[r],v[l]+v[r]+d[r]-d[l]); chmax(memor[l],v[l]+v[r]+d[r]-d[l]); } rep(i,n-1)chmax(memol[i+1],memol[i]); for(int i=n-1;i>0;i--)chmax(memor[i-1],memor[i]); auto calc1=[&](ll l,ll r){ ll vl=v[l]; rep(i,l)chmax(v[l],d[l]-d[i]+v[i]); ll cycle_len=c+d[r]-d[l]; deque<P> deq; ll ma=-inf,t=l,res=memol[l]; REP(i,l,r+1){ while(t<i&&(d[i]-d[t])*2>cycle_len){ chmax(ma,d[t]+v[t]+cycle_len); if(deq.size()&&deq.front().fi==t)deq.pop_front(); t++; } if(deq.size())chmax(res,d[i]+v[i]+deq.front().se); chmax(res,-d[i]+v[i]+ma); while(deq.size()&&deq.back().se<=v[i]-d[i])deq.pop_back(); deq.pb(i,v[i]-d[i]); } v[l]=vl; return res; }; auto calc2=[&](ll l,ll r){ ll vr=-inf,res=memor[r]; REP(i,r+1,n)chmax(vr,d[i]-d[r]+v[i]); rep(i,r)chmax(res,min(d[r]-d[i]+v[i],c+abs(d[l]-d[i])+v[i])+vr); return res; }; rep(i,n)REP(j,i+1,n)cout<<i<<' '<<j<<' '<<calc1(i,j)<<' '<<calc2(i,j)<<endl; ll ans=inf; rep(i,n)REP(j,i+1,n)chmin(ans,max(calc1(i,j),calc2(i,j))); return ans; } long long find_shortcut2(int n, std::vector<int> l_, std::vector<int> d_, int c){ vi d(n),v(n); rep(i,n-1)d[i+1]=d[i]+l_[i]; rep(i,n)v[i]=d_[i]; vi memol(n),memor(n); rep(l,n)REP(r,l+1,n){ chmax(memol[r],v[l]+v[r]+d[r]-d[l]); chmax(memor[l],v[l]+v[r]+d[r]-d[l]); } rep(i,n-1)chmax(memol[i+1],memol[i]); for(int i=n-1;i>0;i--)chmax(memor[i-1],memor[i]); auto calc1=[&](ll l,ll r){ ll vl=v[l]; rep(i,l)chmax(v[l],d[l]-d[i]+v[i]); ll cycle_len=c+d[r]-d[l]; deque<P> deq; ll ma=-inf,t=l,res=memol[l]; REP(i,l,r+1){ while(t<i&&(d[i]-d[t])*2>cycle_len){ chmax(ma,d[t]+v[t]+cycle_len); if(deq.size()&&deq.front().fi==t)deq.pop_front(); t++; } if(deq.size())chmax(res,d[i]+v[i]+deq.front().se); chmax(res,-d[i]+v[i]+ma); while(deq.size()&&deq.back().se<=v[i]-d[i])deq.pop_back(); deq.pb(i,v[i]-d[i]); } v[l]=vl; return res; }; auto calc2=[&](ll l,ll r){ ll vr=-inf,res=memor[r]; REP(i,r+1,n)chmax(vr,d[i]-d[r]+v[i]); rep(i,r)chmax(res,min(d[r]-d[i]+v[i],c+abs(d[l]-d[i])+v[i])+vr); return res; }; ll ans=inf; ll r=0; rep(l,n-1){ r=max(r-1,l+1); while(true){ ll a=calc1(l,r),b=calc2(l,r); chmin(ans,max(a,b)); if(a>=b)break; r++; } } return ans; } long long find_shortcut(int n, std::vector<int> l_, std::vector<int> d_, int c){ vi d(n),v(n); rep(i,n-1)d[i+1]=d[i]+l_[i]; rep(i,n)v[i]=d_[i]; vi memol(n),memor(n); rep(l,n)REP(r,l+1,n){ chmax(memol[r],v[l]+v[r]+d[r]-d[l]); chmax(memor[l],v[l]+v[r]+d[r]-d[l]); } rep(i,n-1)chmax(memol[i+1],memol[i]); for(int i=n-1;i>0;i--)chmax(memor[i-1],memor[i]); auto calc1=[&](ll l,ll r){ ll vl=v[l]; rep(i,l)chmax(v[l],d[l]-d[i]+v[i]); ll cycle_len=c+d[r]-d[l]; deque<P> deq; ll ma=-inf,t=l,res=memol[l]; REP(i,l,r+1){ while(t<i&&(d[i]-d[t])*2>cycle_len){ chmax(ma,d[t]+v[t]+cycle_len); if(deq.size()&&deq.front().fi==t)deq.pop_front(); t++; } if(deq.size())chmax(res,d[i]+v[i]+deq.front().se); chmax(res,-d[i]+v[i]+ma); while(deq.size()&&deq.back().se<=v[i]-d[i])deq.pop_back(); deq.pb(i,v[i]-d[i]); } v[l]=vl; return res; }; auto calc2=[&](ll l,ll r){ ll vr=-inf,res=memor[r]; REP(i,r+1,n)chmax(vr,d[i]-d[r]+v[i]); rep(i,r)chmax(res,min(d[r]-d[i]+v[i],c+abs(d[l]-d[i])+v[i])+vr); return res; }; ll ans=inf; rep(l,n-1){ ll ok=l,ng=n-1; while(ng-ok>1){ ll md=(ok+ng)/2; if(calc1(l,md)>=calc2(l,md))ng=md; else ok=md; } if(l!=ok)chmin(ans,max(calc1(l,ok),calc2(l,ok))); chmin(ans,max(calc1(l,ng),calc2(l,ng))); } return ans; } /* int main(){ int n=7,c=1; vector<int> l={4,8,5,10,1,8},d={8,3,6,4,3,9,3}; ll a=find_shortcut1(n,l,d,c); ll b=find_shortcut(n,l,d,c); out(a);out(b); /* rep(tt,10000){ int n=rand()%7+2,c=rand()%10; vector<int> l(n-1),d(n); rep(i,n-1)l[i]=rand()%10+1; rep(i,n)d[i]=rand()%10; ll a=find_shortcut1(n,l,d,c); ll b=find_shortcut(n,l,d,c); if(a!=b){ out(n); out(c); outv(l);outv(d); out(a);out(b); break; } } */ // }

컴파일 시 표준 에러 (stderr) 메시지

shortcut.cpp:175:5: warning: "/*" within comment [-Wcomment]
  175 |     /*
      |
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...