# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
772316 |
2023-07-04T01:08:40 Z |
cig32 |
Candies (JOI18_candies) |
C++17 |
|
218 ms |
23352 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;
//cout<<"Move "<<i<<": "<<idx<<"\n";
ans[i]=ans[i-1]+(*ms.rbegin()).first;
ms.erase(*ms.rbegin());
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;
//cout<<vt[idx].lc<<" "<<vt[idx].rc<<" "<<vt[idx].val<<"\n";
if(vt[idx].lc!=-1) {
ms.erase({vt[vt[idx].lc].val, vt[idx].lc});
vt[vt[idx].lc].rc=-1;
}
if(vt[idx].rc!=-1) {
ms.erase({vt[vt[idx].rc].val, vt[idx].rc});
vt[vt[idx].rc].lc=-1;
}
if(lb!=-1) {
vt[lb].rc=-1;
}
if(rb!=-1) {
vt[rb].lc=-1;
}
if(vt[idx].lc==-1 || vt[idx].rc==-1) continue;
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;
}
if(rb!=-1) {
vt[rb].lc=idx;
}
vt[idx].lc=lb;
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 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
488 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
488 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
218 ms |
23224 KB |
Output is correct |
22 |
Correct |
193 ms |
23244 KB |
Output is correct |
23 |
Correct |
178 ms |
23240 KB |
Output is correct |
24 |
Correct |
106 ms |
23120 KB |
Output is correct |
25 |
Correct |
92 ms |
23104 KB |
Output is correct |
26 |
Correct |
97 ms |
23116 KB |
Output is correct |
27 |
Correct |
111 ms |
23288 KB |
Output is correct |
28 |
Correct |
111 ms |
23300 KB |
Output is correct |
29 |
Correct |
105 ms |
23256 KB |
Output is correct |
30 |
Correct |
109 ms |
23240 KB |
Output is correct |
31 |
Correct |
115 ms |
23256 KB |
Output is correct |
32 |
Correct |
112 ms |
23256 KB |
Output is correct |
33 |
Correct |
150 ms |
22988 KB |
Output is correct |
34 |
Correct |
152 ms |
23056 KB |
Output is correct |
35 |
Correct |
183 ms |
23104 KB |
Output is correct |
36 |
Correct |
197 ms |
23232 KB |
Output is correct |
37 |
Correct |
198 ms |
23224 KB |
Output is correct |
38 |
Correct |
207 ms |
23224 KB |
Output is correct |
39 |
Correct |
93 ms |
23120 KB |
Output is correct |
40 |
Correct |
89 ms |
23048 KB |
Output is correct |
41 |
Correct |
96 ms |
23104 KB |
Output is correct |
42 |
Correct |
106 ms |
23260 KB |
Output is correct |
43 |
Correct |
103 ms |
23340 KB |
Output is correct |
44 |
Correct |
107 ms |
23244 KB |
Output is correct |
45 |
Correct |
126 ms |
23352 KB |
Output is correct |
46 |
Correct |
113 ms |
23244 KB |
Output is correct |
47 |
Correct |
107 ms |
23244 KB |
Output is correct |
48 |
Correct |
149 ms |
22984 KB |
Output is correct |
49 |
Correct |
144 ms |
23048 KB |
Output is correct |
50 |
Correct |
155 ms |
22980 KB |
Output is correct |