# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
770985 |
2023-07-02T09:21:32 Z |
cig32 |
Candies (JOI18_candies) |
C++17 |
|
1 ms |
468 KB |
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 1e6 + 10;
const int MOD = 998244353;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
struct node {
int rc,lc,val;
};
vector<node>vt;
void solve(int tc){
int n;
cin>>n;
int k=(n+1)/2;
int a[n+1];
for(int i=1;i<=n;i++)cin>>a[i];
vt.resize(n+2);
multiset<pair<int,int>>ms;
for(int i=1;i<=n;i++){
vt[i].lc=(i==1?-1:i-1);
vt[i].rc=(i==n?-1:i+1);
vt[i].val=a[i];
ms.insert({vt[i].val,i});
}
int ans[k+1];
ans[0]=0;
for(int i=1;i<=k;i++){
int idx=(*ms.rbegin()).second;
ans[i]=ans[i-1]+(*ms.rbegin()).first;
int lb=-1,rb=-1;
if(vt[idx].lc!=-1 && vt[vt[idx].lc].lc!=-1) lb=vt[vt[idx].lc].lc;
if(vt[idx].rc!=-1 && vt[vt[idx].rc].rc!=-1) rb=vt[vt[idx].rc].rc;
ms.erase(*ms.rbegin());
if(vt[idx].lc!=-1) ms.erase({vt[vt[idx].lc].val, vt[idx].lc});
if(vt[idx].rc!=-1) ms.erase({vt[vt[idx].rc].val, vt[idx].rc});
int nw=0;
if(vt[idx].lc!=-1) nw+=vt[vt[idx].lc].val;
if(vt[idx].rc!=-1) nw+=vt[vt[idx].rc].val;
nw-=vt[idx].val;
if(lb!=-1) {
vt[lb].rc=idx;
vt[idx].lc=lb;
}
if(rb!=-1) {
vt[rb].lc=idx;
vt[idx].rc=rb;
}
vt[idx].val=nw;
ms.insert({vt[idx].val,idx});
}
for(int i=1; i<=k; i++) cout << ans[i] << "\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |