Submission #60589

#TimeUsernameProblemLanguageResultExecution timeMemory
60589dukati8Palembang Bridges (APIO15_bridge)C++14
0 / 100
4 ms580 KiB
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for (int i=a; i<int(b); i++) #define all(x) x.begin(),x.end() vector <pair<long long,long long> > travels; vector <long long> points; long long cost(long long i) { long long point=i; long long ans=0; for (auto a:travels) { long long c,d; c=a.first; d=a.second; assert (c<=d); if (point >d) ans+=2*(point-d); if (point<c) ans+=2*(c-point); } return ans; } int main() { vector<long long> unip; int k,n; cin >> k >> n; long long sum=0; rep(i,0,n) { char a,b; long long c,d; cin >> a >> c >> b >> d; if (a==b) { sum+=abs(c-d); } else { travels.push_back(make_pair(min(c,d),max(c,d))); sum+=1+abs(c-d); unip.push_back(c); unip.push_back(d); } } for (auto x:unip) { points.push_back(x); } if (k==1) { sort(all(points)); int lo,hi; lo=0; hi=points.size()-1; while (lo+50<hi) { int mid1=lo+(hi-lo)/3; int mid2=lo+(2*hi-2*lo)/3; long long val1,val2; val1=cost(mid1); val2=cost(mid2); if (val1<val2) hi=mid2; else lo=mid1; } long long best=1000000000000000; rep (i,lo,hi+1) { best=min(best,cost(points[i])); } if (best==1000000000000000) best=0; cout << best+sum << endl; } }
#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...