Submission #545982

#TimeUsernameProblemLanguageResultExecution timeMemory
545982Ooops_sorryPalembang Bridges (APIO15_bridge)C++14
100 / 100
309 ms15284 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define ld double #define ll __int128 mt19937 rnd(51); int suml = 0, sumr = 0, ind = 0; multiset<int> l, r; void add(int j) { if (j < ind) { l.insert(j); suml += j; } else { r.insert(j); sumr += j; } if (l.size() > r.size()) { int val = *l.rbegin(); suml -= val; l.erase(l.find(val)); ind = val; r.insert(val); sumr += val; } else if (l.size() < r.size()) { int val = *r.begin(); sumr -= val; r.erase(r.find(val)); ind = val; l.insert(val); suml += val; } } signed main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif // LOCAL ios_base::sync_with_stdio(0); cin.tie(0); int k, n, ans = 0; cin >> k >> n; vector<pair<int,int>> u; for (int i = 0; i < n; i++) { char p, q; int s, t; cin >> p >> s >> q >> t; s--, t--; if (p == q) { ans += abs(s - t); } else { u.pb({min(s, t), max(s, t)}); ans++; } } sort(u.begin(), u.end(), [&](pair<int,int> a, pair<int,int> b){return a.first + a.second < b.first + b.second;}); int m = u.size(); vector<int> res(m), ress(m); for (int i = 0; i < m; i++) { add(u[i].first); add(u[i].second); res[i] = ind * (int)(l.size()) - suml + sumr - (int)(r.size()) * ind; } suml = 0, sumr = 0, l.clear(), r.clear(); for (int i = m - 1; i >= 0; i--) { add(u[i].first); add(u[i].second); ress[i] = ind * (int)(l.size()) - suml + sumr - (int)(r.size()) * ind; } int mn = (res.size() > 0 ? res.back() : 0); if (k == 2) { for (int i = 1; i < m; i++) { mn = min(mn, res[i - 1] + ress[i]); } } cout << ans + mn << endl; 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...