Submission #316596

#TimeUsernameProblemLanguageResultExecution timeMemory
316596tushar_2658Palembang Bridges (APIO15_bridge)C++14
100 / 100
323 ms24808 KiB
#include "bits/stdc++.h" using namespace std; const int maxn = 200005; using ll = long long; struct DS { multiset<int> P, Q; ll sumP, sumQ; DS(){ sumP = sumQ = 0; } void add(int x){ if(P.empty()){ P.insert(x); sumP += x; }else if(*P.rbegin() < x){ Q.insert(x); sumQ += x; }else { P.insert(x); sumP += x; } if(P.size() < Q.size()){ sumP += *Q.begin(); sumQ -= *Q.begin(); P.insert(*Q.begin()); Q.erase(Q.begin()); } if(P.size() > Q.size() + 1){ sumP -= *P.rbegin(); sumQ += *P.rbegin(); Q.insert(*P.rbegin()); P.erase(P.find(*P.rbegin())); } } ll get(){ ll mid = *P.rbegin(); ll ans = sumQ - sumP; ans -= mid * Q.size(); ans += mid * P.size(); return ans; } }; int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(0); int k, n; cin >> k >> n; vector<pair<ll, ll>> v; ll ans = 0; for(int i = 0; i < n; ++i){ char c, c1; ll x, y; cin >> c >> x >> c1 >> y; if(c == c1){ ans += llabs(x - y); }else { ++ans; if(x > y)swap(x, y); v.push_back({x, y}); } } if(v.empty()){ cout << ans << endl; return 0; } n = v.size(); sort(v.begin(), v.end(), [&](pair<ll, ll> x, pair<ll, ll> y){ return x.first + x.second < y.first + y.second; }); DS S, T; vector<ll> pref(n + 1), suff(n + 1); for(int i = 0; i < n; ++i){ S.add(v[i].first); S.add(v[i].second); pref[i] = S.get(); } for(int i = n - 1; i >= 0; --i){ T.add(v[i].first); T.add(v[i].second); suff[i] = T.get(); } ll res = 1e18; for(int i = 0; i < n; ++i){ res = min(res, pref[i] + suff[i + 1]); } if(k == 1){ printf("%lld\n", pref[n - 1] + ans); }else { printf("%lld\n", ans + res); } return 0; }
#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...