Submission #923092

#TimeUsernameProblemLanguageResultExecution timeMemory
923092quanlt206Palembang Bridges (APIO15_bridge)C++17
100 / 100
74 ms6092 KiB
#include<bits/stdc++.h> #define X first #define Y second #define all(x) begin(x), end(x) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define FORD(i, b, a) for(int i = (b); i >= (a); i--) #define REP(i, a, b) for (int i = (a); i < (b); i++) #define mxx max_element #define mnn min_element #define SQR(x) (1LL * (x) * (x)) #define MASK(i) (1LL << (i)) #define Point Vector #define left Left #define right Right #define div Div using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; typedef pair<db, db> pdb; typedef pair<ld, ld> pld; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, pll> plll; typedef pair<ll, int> pli; typedef pair<ll, pii> plii; template<class A, class B> bool maximize(A& x, B y) { if (x < y) return x = y, true; else return false; } template<class A, class B> bool minimize(A& x, B y) { if (x > y) return x = y, true; else return false; } /* END OF TEMPLATE */ const int N = 3e5 + 7; int n, k; vector<pii> seg; ll res = 0; priority_queue<int, vector<int>> left; priority_queue<int, vector<int>, greater<int>> right; ll lsum = 0, rsum = 0; ll pre[N], suf[N]; void insert_value(int x) { int median = (left.empty() ? 1000000007 : left.top()); if (x <= median) { left.push(x); lsum+=x; } else { right.push(x); rsum+=x; } if (left.size() == right.size()) return ; if ((int)left.size() > (int)right.size() + 1) { lsum-=left.top(); rsum+=left.top(); right.push(left.top()); left.pop(); } else if ((int)left.size() < (int)right.size()) { rsum-=right.top(); lsum+=right.top(); left.push(right.top()); right.pop(); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>k>>n; FOR(i, 1, n) { char P, Q; int A, B; cin>>P>>A>>Q>>B; if (P == Q) res+=abs(A - B); if (P != Q) seg.push_back({min(A, B), max(A, B)}), res++; } if (seg.empty()) return cout<<res, 0; seg.push_back({-1, -1}); sort(all(seg), [](pii a, pii b) { return a.X + a.Y < b.X + b.Y; }); pre[0] = 0; n = (int)seg.size() - 1; FOR(i, 1, n) { insert_value(seg[i].X); insert_value(seg[i].Y); pre[i] = rsum - lsum; } if (k == 1) return cout<<pre[n] + res, 0; while (!left.empty()) left.pop(); while (!right.empty()) right.pop(); lsum = 0; rsum = 0; ll ans = 1e18; FORD(i, n, 1) { insert_value(seg[i].X); insert_value(seg[i].Y); minimize(ans, rsum - lsum + res + pre[i - 1]); } cout<<ans; 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...