Submission #39508

#TimeUsernameProblemLanguageResultExecution timeMemory
39508qoo2p5Palembang Bridges (APIO15_bridge)C++14
22 / 100
86 ms11988 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = (int) 1e9 + 123; const ll LINF = (ll) 1e18 + 123; #define rep(i, s, t) for (auto i = (s); i < (t); ++(i)) #define per(i, s, t) for (auto i = (s); i >= (t); --(i)) #define sz(x) ((int) (x).size()) #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() bool mini(auto &x, const auto &y) { if (y < x) { x = y; return 1; } return 0; } bool maxi(auto &x, const auto &y) { if (y < x) { x = y; return 1; } return 0; } void run(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); run(); return 0; } const int N = (int) 1e5 + 123; int k, n; vector<pair<ll, ll>> a; vector<ll> cool; ll total; void solve1() { vector<ll> p; { for (auto &it : a) { p.pb(it.first); p.pb(it.second); } sort(all(p)); } int k = sz(p) / 2; ll x = p[k]; ll ans = 0; for (ll y : p) ans += abs(x - y); ans += total; cout << ans << "\n"; } ll pref[N], suff[N]; ll solve() { sort(all(a), [] (const pair<ll, ll> &x, const pair<ll, ll> &y) { return x.first + x.second < y.first + y.second; }); { ll sum_l = 0, sum_r = 0; multiset<ll> L, R; multiset<ll> q; rep(i, 0, n) { R.insert(a[i].first); R.insert(a[i].second); sum_r += a[i].first + a[i].second; q.insert(a[i].first); q.insert(a[i].second); while (sz(q) > (sz(L) + sz(R) + 1) / 2) { q.erase(q.find(*q.rbegin())); } ll mid = *q.rbegin(); while (sz(R) && *R.begin() <= mid) { ll el = *R.begin(); sum_r -= el; sum_l += el; L.insert(el); R.erase(R.find(el)); } pref[i] = (mid * sz(L) - sum_l) + (sum_r - mid * sz(R)); } } { ll sum_l = 0, sum_r = 0; multiset<ll> L, R; multiset<ll> q; per(i, n - 1, 0) { R.insert(a[i].first); R.insert(a[i].second); sum_r += a[i].first + a[i].second; q.insert(a[i].first); q.insert(a[i].second); while (sz(q) > (sz(L) + sz(R) + 1) / 2) { q.erase(q.find(*q.rbegin())); } ll mid = *q.rbegin(); while (sz(R) && *R.begin() <= mid) { ll el = *R.begin(); sum_r -= el; sum_l += el; L.insert(el); R.erase(R.find(el)); } suff[i] = (mid * sz(L) - sum_l) + (sum_r - mid * sz(R)); } } ll ans = LINF; rep(i, 0, n) { mini(ans, pref[i] + suff[i + 1]); } return ans; } void run() { cin >> k >> n; rep(i, 0, n) { char t1, t2; ll x1, x2; cin >> t1 >> x1 >> t2 >> x2; if (t1 == t2) { total += abs(x1 - x2); } else { if (x1 > x2) { swap(t1, t2); swap(x1, x2); } a.pb({x1, x2}); cool.pb(x1); cool.pb(x2); } } sort(all(cool)); cool.resize(unique(all(cool)) - cool.begin()); sort(all(a)); n = sz(a); total += n; if (k == 1) { solve1(); return; } if (n == 0) { cout << total << "\n"; return; } ll ans = solve(); ans += total; cout << ans << "\n"; }
#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...