Submission #637953

#TimeUsernameProblemLanguageResultExecution timeMemory
637953ojoConmigoPalembang Bridges (APIO15_bridge)C++17
0 / 100
2075 ms408 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll k,n; vector<pair<ll,ll>> v; vector<pair<int,pair<ll,ll>>> ordx,ordy; ll f(ll puente){ //cout << "puente: " << puente << endl; ll res = 0; for(auto par : v){ ll x = par.first; ll y = par.second; if(x <= puente && puente <= y){ res += (y-x+1); }else{ res += (abs(puente - x) + abs(puente - y) + 1); } //cout << res << endl; } return res; } ll f2(ll puente,ll sol){ ll lo = 0, hi = ordx.size()-1; ll res1 = ordx.size()-1,res2 = 0; while(lo <= hi){ int mid = (lo+hi)/2; if(ordx[mid].second.first > puente){ res1 = mid; hi = mid-1; }else{ lo = mid+1; } } lo = 0; hi = ordy.size()-1; while(lo <= hi){ int mid = (lo+hi)/2; if(ordy[mid].second.second < puente){ res2 = mid; lo = mid+1; }else{ hi = mid-1; } } vector<bool> usado(n,false); for(int i=0; i<res1+1; i++){ ll x = ordx[i].second.first; ll y = ordx[i].second.second; ll ind = ordx[i].first; sol -= (y-x+1); sol += (abs(puente - x) + abs(puente - y) + 1); usado[ind] = true; } for(int i=res2; i<(int)ordy.size(); i++){ ll ind = ordy[i].first; if(usado[ind])continue; ll x = ordy[i].second.first; ll y = ordy[i].second.second; sol -= (y-x+1); sol += (abs(puente - x) + abs(puente - y) + 1); } //cout << puente << " " << sol << endl; return sol; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> k >> n; ll sol = 0; ll mayor = 0; for(int i=0; i<n; i++){ ll x,y; char a,b; cin >> a >> x >> b >> y; mayor = max(mayor,x); mayor = max(mayor,y); if(a == b){ sol += abs(x-y); }else{ if(x > y){ swap(x,y); } v.push_back({x,y}); ordx.push_back({i,{x,y}}); ordy.push_back({i,{x,y}}); //sol += (y-x+1); } } //ordx = ordy = v; /* sort(begin(ordx),end(ordx),[] (const auto & a, const auto & b){ return a.second.first <= b.second.first; }); sort(begin(ordy),end(ordy),[] (const auto & a, const auto & b){ return a.second.second <= b.second.second; }); */ //Ternary search ll lb = 0, ub = mayor; while(lb+2 < ub){ //cout << lb << " " << ub << endl; ll m1 = lb + (lb+ub)/3; ll m2 = ub - (lb+ub)/3; //cout << m1 << " " << f(m1) << " " << m2 << " " << f(m2) << endl; ll v1 = f(m1); ll v2 = f(m2); if(m1 > m2)break; if(v1 <= v2){ ub = m2; }else{ lb = m1; } } ll res = 1e18; for(ll i=lb; i<=ub; i++){ res = min(res,f(i)); } cout << res+sol << 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...