Submission #936358

#TimeUsernameProblemLanguageResultExecution timeMemory
936358velislavgarkovCandies (JOI18_candies)C++14
0 / 100
1 ms4700 KiB
#include <iostream> #include <queue> using namespace std; #define endl '\n' const int MAXN=2e5+10; struct Node { long long pl; int l, r; int idx; bool friend operator < (Node a, Node b) { return a.pl<b.pl; } }; priority_queue <Node> q; int lef[MAXN], ri[MAXN], ind[MAXN]; long long a[MAXN], pref[MAXN]; bool used[MAXN]; long long find_sum(int l, int r) { if (l==1) return pref[r]; return pref[r]-pref[l-2]; } int main () { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i=1;i<=n;i++) { cin >> a[i]; if (i==1) pref[i]=a[i]; else pref[i]=pref[i-2]+a[i]; lef[i]=ri[i]=i; q.push({a[i],i,i,i}); } long long sum=0; for (int i=1;i<=n/2+n%2;i++) { while (used[q.top().idx]) q.pop(); auto p=q.top(); q.pop(); sum+=p.pl; cout << sum << endl; used[p.l-1]=used[p.r+1]=true; if (p.l==1) { used[ind[ri[p.r+1]]]=true; lef[ri[p.r+1]]=0; continue; } if (p.r==n) { used[ind[lef[p.l-1]]]=true; ri[lef[p.l-1]]=n+1; continue; } int newl, newr; newl=lef[p.l-1]; newr=ri[p.r+1]; ri[newl]=newr; lef[newr]=newl; used[ind[newl]]=used[ind[newr]]=true; ind[newl]=ind[newr]=p.idx; long long ch=find_sum(newl,newr)-find_sum(newl+1,newr-1); q.push({ch,newl,newr,p.idx}); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...