제출 #637926

#제출 시각아이디문제언어결과실행 시간메모리
637926ojoConmigoPalembang Bridges (APIO15_bridge)C++17
0 / 100
2077 ms464 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<pair<ll,ll>> v; 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; } int main(){ ios::sync_with_stdio(false); cin.tie(0); ll k,n; 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}); } } //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; if(m1 > m2)break; if(f(m1) <= f(m2)){ ub = m2-1; }else{ lb = m1+1; } } 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...