Submission #660579

#TimeUsernameProblemLanguageResultExecution timeMemory
660579BananWiring (IOI17_wiring)C++17
0 / 100
1 ms212 KiB
#include "wiring.h" #include <bits/stdc++.h> using namespace std; using ll = long long; #define double long double #define endl '\n' #define sz(a) (int)a.size() #define pb push_back #define fs first #define sc second #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() ll const INF = LONG_LONG_MAX; long long min_total_length(std::vector<int> r, std::vector<int> b) { ll n=sz(r), m=sz(b); if(n>m){swap(n, m);swap(r, b);} set<ll> incr; set<ll, greater<>> decr; for(int i=0;i<m;i++) { incr.insert(b[i]); decr.insert(b[i]); } map<int, bool> mark; ll ans=0; for(int i=0;i<n;i++) { auto it1=incr.lower_bound(r[i]), it2=decr.lower_bound(r[i]); ll v1=(*it1), v2=(*it2); if(abs(v1-r[i])==abs(v2-r[i])) { ans+=abs(v2-r[i]); incr.erase(v2); decr.erase(v2); mark[v2]=1; } else if(abs(v1-r[i])<abs(v2-r[i])) { ans+=abs(v1-r[i]); incr.erase(v1); decr.erase(v1); mark[v1]=1; } else if(abs(v1-r[i])>abs(v2-r[i])) { ans+=abs(v2-r[i]); incr.erase(v2); decr.erase(v2); mark[v2]=1; } } for(int i=0;i<m;i++) { if(!mark[b[i]]) { int lo=0, hi=n-1, mid=(lo+hi)/2, ind1=-1; while(lo<=hi) { if(r[mid]<b[i]){ind1=mid;lo=mid+1;mid=(lo+hi)/2;} else{hi=mid-1;mid=(lo+hi)/2;} } lo=0, hi=n-1, mid=(lo+hi)/2; int ind2=-1; while(lo<=hi) { if(r[mid]>b[i]){ind2=mid;lo=mid+1;mid=(lo+hi)/2;} else{hi=mid-1;mid=(lo+hi)/2;} } if(ind1==-1){ans+=abs(r[ind2]-b[i]);} else if(ind2){ans+=abs(r[ind1]-b[i]);} else{ans+=max(abs(r[ind2]-b[i]), abs(r[ind1]-b[i]));} } } return ans; }
#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...