Submission #260856

#TimeUsernameProblemLanguageResultExecution timeMemory
260856jeqchoPalembang Bridges (APIO15_bridge)C++17
31 / 100
2086 ms2456 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<pair<int,int>> vpi; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) #define pb push_back #define rsz resize #define sz(x) int(x.size()) #define all(x) begin(x), end(x) #define f first #define s second vpi cross; ll build(int bri) { ll val=0; trav(e, cross) { val += (ll)(e.s-e.f+1); if(bri>e.s) { val += (ll)(bri - e.s) * (ll)2; } else if(bri<e.f) { val += (ll)(e.f - bri) * (ll)2; } } return val; } ll build2(int lb, int rb) { ll val=0; ll lcand,rcand; trav(e, cross) { val += (ll)(e.s-e.f+1); lcand=rcand=0; if(lb>e.s) { lcand += (ll)(lb - e.s) * (ll)2; } else if(lb<e.f) { lcand += (ll)(e.f - lb) * (ll)2; } if(rb>e.s) { rcand += (ll)(rb - e.s) * (ll)2; } else if(rb<e.f) { rcand += (ll)(e.f - rb) * (ll)2; } val += min(lcand,rcand); } return val; } bool ok(int now) { ll g = build(now+1) - build(now); if(g<0)return 1; else return 0; } bool check(int lb, int rb, int lef, int rig) { if(lef) if(build2(lb+lef,rb+rig)-build2(lb,rb)<0)return 1; else return 0; } ll test(int lr, int rr) { int rb = cross[rr].s; int lef = cross[lr].f; int rig = cross[lr].s; int mid; while(lef<=rig) { mid=(lef+rig)/2; if(check(mid, rb, 1,0))lef=mid+1; else rig=mid-1; } int lb = lef; lef = cross[rr].f; rig = cross[rr].s; while(lef<=rig) { mid=(lef+rig)/2; if(check(lb, mid, 0,1))lef=mid+1; else rig=mid-1; } rb = lef; ll ans=2e18; FOR(i,max(0,lb-5),min((int)1e9+1,lb+5)) { FOR(j,max(0,rb-5),min((int)1e9+1,rb+5)) { ans = min(ans, build2(lb,rb)); } } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int K,N; cin>>K>>N; char P[N],Q[N]; int S[N],T[N]; ll base=0; F0R(i,N) { cin>>P[i]>>S[i]>>Q[i]>>T[i]; if(P[i]==Q[i]) { base += (ll)( max(S[i],T[i]) - min(S[i],T[i]) ); } else { cross.pb({min(S[i],T[i]), max(S[i],T[i])}); } } ll ans; if(K==2) { ans = 2e14+5; sort(all(cross)); F0R(i,sz(cross)) { FOR(j,i+1,sz(cross)) { ans=min(ans,test(i,j)); } } } else { int lef=0,rig=1e9; int mid; while(lef<=rig) { mid=(lef+rig)/2; if(ok(mid))lef=mid+1; else rig=mid-1; } ans = 2e14+5; FOR(i,max(0,lef-10),min((int)1e9+1,lef+10)) { ans=min(ans,build(i)); } } if(ans==2e14+5)ans=0; ans+=base; cout<<ans<<'\n'; return 0; }

Compilation message (stderr)

bridge.cpp: In function 'bool check(int, int, int, int)':
bridge.cpp:80:7: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
     if(lef)
       ^
bridge.cpp:83:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
#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...