Submission #1117208

#TimeUsernameProblemLanguageResultExecution timeMemory
1117208ShaShiPalembang Bridges (APIO15_bridge)C++17
100 / 100
91 ms57016 KiB
#include <bits/stdc++.h> #define int long long // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #define F first #define S second #define mp make_pair #define pb push_back #define all(x) x.begin(), x.end() #define kill(x) cout << x << "\n", exit(0); #define pii pair<int, int> #define pll pair<long long, long long> #define endl "\n" using namespace std; typedef long long ll; // typedef __int128_t lll; typedef long double ld; const int MAXN = (int)1e6 + 7; const int MOD = 998244353; const ll INF = (ll)1e18 + 7; int n, m, k, tmp, t, tmp2, tmp3, tmp4, u, v, w, flag, q, ans, N, res, pre_ans; char ch, ch2; vector<int> vec[2][MAXN], cmp; vector<pair<int, pii>> ord; int ps[2][MAXN]; /* Median DS */ priority_queue<int> lpq; priority_queue<int, vector<int>, greater<int>> rpq; int lsum, rsum; inline void add(int x) { int median = (lpq.size()? lpq.top() : INF); if (x <= median) lpq.push(x), lsum += x; else rpq.push(x), rsum += x; if (lpq.size() < rpq.size()) { rsum -= rpq.top(); lsum += rpq.top(); lpq.push(rpq.top()); rpq.pop(); } else if (rpq.size()+1 < lpq.size()) { lsum -= lpq.top(); rsum += lpq.top(); rpq.push(lpq.top()); lpq.pop(); } } /* Median DS */ int32_t main() { #ifdef LOCAL freopen("inp.in", "r", stdin); freopen("res.out", "w", stdout); #else ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif cin >> k >> n; for (int i=1; i<=n; i++) { cin >> ch >> u >> ch2 >> v; if (ch == ch2) pre_ans += abs(u-v); else ord.pb({u+v, {u, v}}); } N = ord.size(); sort(all(ord)); pre_ans += N; lsum = rsum = 0; for (int i=1; i<=N; i++) { add(ord[i-1].S.F); add(ord[i-1].S.S); ps[0][i] = rsum - lsum; } ans = ps[0][N]; if (k == 1) kill(ans+pre_ans); lsum = rsum = 0; while (lpq.size()) lpq.pop(); while (rpq.size()) rpq.pop(); for (int i=N; i>=1; i--) { add(ord[i-1].S.F); add(ord[i-1].S.S); ans = min(ans, rsum - lsum + ps[0][i-1]); } cout << pre_ans+ans << 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...