Submission #702107

#TimeUsernameProblemLanguageResultExecution timeMemory
702107ymmCandies (JOI18_candies)C++17
100 / 100
309 ms23340 KiB
#include <bits/stdc++.h> #define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x) #define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x) typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll , ll > pll; using namespace std; const int N = 200'010; ll a[N]; struct Obj { int l, r; ll val0, val1; } obj[N]; int rt[N]; ll val(int x) { return obj[x].val0 - obj[x].val1; } set<pair<ll, int>, greater<pair<ll, int>>> pq; int main() { cin.tie(0) -> sync_with_stdio(false); int n; cin >> n; Loop (i,0,n+2) { if (i==0 || i==n+1) a[i] = -1e18; else cin >> a[i]; obj[i].l = obj[i].r = i; obj[i].val0 = a[i]; pq.insert({val(i), i}); rt[i] = i; } ll sum = 0; Loop (i,0,(n+1)/2) { int p = pq.begin()->second; sum += val(p); cout << sum << '\n'; int lrt = rt[obj[p].l-1]; int rrt = rt[obj[p].r+1]; pq.erase({val(p), p}); pq.erase({val(lrt), lrt}); pq.erase({val(rrt), rrt}); rt[obj[lrt].l] = p; rt[obj[rrt].r] = p; swap(obj[p].val0, obj[p].val1); obj[p].val0 += obj[lrt].val0; obj[p].val0 += obj[rrt].val0; obj[p].val1 += obj[lrt].val1; obj[p].val1 += obj[rrt].val1; obj[p].l = obj[lrt].l; obj[p].r = obj[rrt].r; pq.insert({val(p), p}); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...