#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
#define Pii pair<int,pii>
using namespace std;
const int N = 2e5 + 5, inf = 1e14; // !
int ans[N], a[N], p[N];
set<pii> S;
set<Pii> s;
struct node {
pair<int, pii> best[2];
pii R[2], L[2];
} t[4 * N], Z;
node merge(node a, node b) {
node c;
for(int t = 0; t < 2; t++) c.R[t] = max(a.R[t], b.R[t]), c.L[t] = min(a.L[t], b.L[t]);
for(int t = 0; t < 2; t++)
c.best[t] = max(a.best[t], b.best[t]),
c.best[t] = max(c.best[t], {{b.R[t].f - a.L[t^1].f}, {a.L[t^1].s + 1, b.R[t].s}});
return c;
}
void build(int u, int l, int r) {
if(l == r) {
t[u].best[0].f = t[u].best[1].f = -inf;
t[u].R[l % 2] = {(l % 2 ? p[l] : -p[l]), l}; t[u].R[1 - l % 2].f = -inf;
t[u].L[l % 2] = {(l % 2 ? -p[l] : p[l]), l}; t[u].L[1 - l % 2].f = inf;
return;
}
int mid = (l + r) / 2;
build(2 * u, l, mid); build(2 * u + 1, mid + 1, r);
t[u] = merge(t[2 * u], t[2 * u + 1]);
}
int n;
node get(int u, int st, int en, int l, int r) {
if(l > en || r < st) return Z;
if(st <= l && r <= en) return t[u];
int mid = (l + r) / 2;
return merge(get(2 * u, st ,en, l, mid), get(2 * u + 1, st, en, mid + 1, r));
}
void add(int l, int r) {
if(l > r) return;
Pii p = get(1, l - 1, r, 0, n).best[l % 2];
S.insert({l, r});
s.insert(p);
}
main(){
cin >> n;
int cur = 0;
vector<int> sum(2);
for(int i = 1; i <= n; i++) {
cin >> a[i];
sum[i % 2] += a[i];
p[i] = p[i - 1] + (i % 2 ? -a[i] : a[i]);
}
for(int t = 0; t < 2; t++) Z.R[t].f = Z.best[t].f = -inf, Z.L[t].f = inf;
build(1, 0, n);
if(n % 2) cur = sum[1], add(1, n);
else {
if(sum[1] <= sum[0]) cur = sum[0];
else cur = sum[1];
vector<int> x(2);
for(int i = 1; i <= n; i ++) {
// i, i + 1 vigebt uechveli eseni gvawyobs
if(i % 2 == 0) cur = max(cur, x[1] + sum[0] - x[0] - a[i]);
x[i % 2] += a[i];
}
if(cur == sum[0]) add(2, n);
else if(cur == sum[1]) add(1, n - 1);
else {
x[0] = x[1] = 0;
for(int i = 1; i < n; i ++) {
if(i % 2 == 0 && cur == x[1] + sum[0] - x[0] - a[i]) {
// cout << i << endl;
add(1, i - 1);
add(i + 2, n);
break;
}
x[i % 2] += a[i];
}
}
}
ans[(n + 1) /2 ] = cur;
for(int i = (n + 1) / 2 - 1; i >= 1; i--) {
Pii p = (*--s.end());
int l = p.s.f, r = p.s.s;
s.erase(p);
cur += p.f; ans[i] = cur;
pii cur = *--S.upper_bound({l + 1, 0});
S.erase(cur);
if(cur.f <= l - 2) add(cur.f, l - 2);
if(cur.s >= r + 2) add(r + 2, cur.s);
if(l < r) add(l + 1, r - 1);
}
for(int i = 1; i <= (n + 1) / 2; i++) cout << ans[i] << " ";
}
Compilation message
candies.cpp:52:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
52 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
852 KB |
Output is correct |
2 |
Correct |
3 ms |
852 KB |
Output is correct |
3 |
Correct |
3 ms |
876 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
844 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
3 ms |
836 KB |
Output is correct |
9 |
Correct |
3 ms |
836 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
4 ms |
832 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
5 ms |
840 KB |
Output is correct |
14 |
Correct |
3 ms |
836 KB |
Output is correct |
15 |
Correct |
3 ms |
840 KB |
Output is correct |
16 |
Correct |
3 ms |
864 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
3 ms |
840 KB |
Output is correct |
19 |
Correct |
3 ms |
840 KB |
Output is correct |
20 |
Correct |
3 ms |
752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
852 KB |
Output is correct |
2 |
Correct |
3 ms |
852 KB |
Output is correct |
3 |
Correct |
3 ms |
876 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
844 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
3 ms |
836 KB |
Output is correct |
9 |
Correct |
3 ms |
836 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
4 ms |
832 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
5 ms |
840 KB |
Output is correct |
14 |
Correct |
3 ms |
836 KB |
Output is correct |
15 |
Correct |
3 ms |
840 KB |
Output is correct |
16 |
Correct |
3 ms |
864 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
3 ms |
840 KB |
Output is correct |
19 |
Correct |
3 ms |
840 KB |
Output is correct |
20 |
Correct |
3 ms |
752 KB |
Output is correct |
21 |
Correct |
364 ms |
69508 KB |
Output is correct |
22 |
Correct |
379 ms |
69584 KB |
Output is correct |
23 |
Correct |
495 ms |
69396 KB |
Output is correct |
24 |
Correct |
260 ms |
66800 KB |
Output is correct |
25 |
Correct |
262 ms |
66840 KB |
Output is correct |
26 |
Correct |
264 ms |
66932 KB |
Output is correct |
27 |
Correct |
276 ms |
69468 KB |
Output is correct |
28 |
Correct |
270 ms |
69484 KB |
Output is correct |
29 |
Correct |
267 ms |
69492 KB |
Output is correct |
30 |
Correct |
279 ms |
69356 KB |
Output is correct |
31 |
Correct |
270 ms |
69360 KB |
Output is correct |
32 |
Correct |
276 ms |
69324 KB |
Output is correct |
33 |
Correct |
291 ms |
69384 KB |
Output is correct |
34 |
Correct |
303 ms |
69324 KB |
Output is correct |
35 |
Correct |
293 ms |
69472 KB |
Output is correct |
36 |
Correct |
341 ms |
69524 KB |
Output is correct |
37 |
Correct |
318 ms |
69500 KB |
Output is correct |
38 |
Correct |
331 ms |
69476 KB |
Output is correct |
39 |
Correct |
254 ms |
66864 KB |
Output is correct |
40 |
Correct |
249 ms |
66844 KB |
Output is correct |
41 |
Correct |
259 ms |
66864 KB |
Output is correct |
42 |
Correct |
273 ms |
69528 KB |
Output is correct |
43 |
Correct |
268 ms |
69512 KB |
Output is correct |
44 |
Correct |
267 ms |
69412 KB |
Output is correct |
45 |
Correct |
291 ms |
69356 KB |
Output is correct |
46 |
Correct |
299 ms |
69292 KB |
Output is correct |
47 |
Correct |
276 ms |
69612 KB |
Output is correct |
48 |
Correct |
288 ms |
69324 KB |
Output is correct |
49 |
Correct |
282 ms |
69380 KB |
Output is correct |
50 |
Correct |
283 ms |
69388 KB |
Output is correct |