Submission #45196

#TimeUsernameProblemLanguageResultExecution timeMemory
45196realityCandies (JOI18_candies)C++17
100 / 100
290 ms11924 KiB
#include "bits/stdc++.h" using namespace std; #define fi first #define se second #define ll long long #define dbg(v) cerr<<#v<<" = "<<v<<'\n' #define vi vector<int> #define vl vector <ll> #define pii pair<int,int> #define mp make_pair #define db long double #define pb push_back #define all(s) s.begin(),s.end() template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} const int N = (int)(1e6) + 5; ll s[N]; int lf[N]; int rg[N]; ll lv[N]; ll rv[N]; int main(void) { int n; cin>>n; n += 2; s[1] = -1e16; s[n] = -1e16; for (int i = 2;i < n;++i) cin>>s[i]; for (int i = 0;i <= n;++i) { lf[i] = rg[i] = -1; lv[i] = rv[i] = -1e16; if (i > 0) lf[i] = i - 1,lv[i] = s[i]; if (i < n) rg[i] = i + 1,rv[i] = s[i + 1]; } priority_queue < pair < ll , pii > > Q; for (int i = 1;i <= n;++i) Q.push(mp(s[i],mp(i - 1,i))); ll answer = 0; n -= 2; for (int cs = 1;cs * 2 <= n + 1;++cs) { const int u = Q.top().se.fi; const int v = Q.top().se.se; const ll cost = Q.top().fi; Q.pop(); if (lf[v] != u || rg[u] != v) { --cs; continue; } answer += cost; int L = lf[u]; int R = rg[v]; lf[u] = rg[v] = -1; if (L != -1 && R != -1) { const ll new_cost = lv[u] + rv[v] - cost; lf[R] = L;rg[L] = R; rv[L] = lv[R] = new_cost; Q.push(mp(new_cost,mp(L,R))); } cout << answer << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...