#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 200200;
ll pref[N], ans, n;
ll get(int l, int r) {
return pref[r] - (l > 1 ? pref[l - 2] : 0ll);
}
ll inv(int l, int r) {
return l == r ? 0ll : get(l + 1, r - 1);
}
ll f(pair < int, int > seg) {
auto &[l, r] = seg;
return (l^r)&1 ? LLONG_MIN : get(l, r) - inv(l, r);
}
set < pair < int, int > > segs;
set < pair < ll, pair < int, int > > > flips;
void flip(int l, int r) {
auto it = segs.find({l, r}), l_seg = it, r_seg = it;
flips.erase(flips.find({f(*it), *it}));
if(it != segs.begin()) {
-- l_seg;
flips.erase(flips.find({f(*l_seg), *l_seg}));
l = l_seg -> first;
segs.erase(l_seg);
}
++ r_seg;
if(r_seg != segs.end()) {
flips.erase(flips.find({f(*r_seg), *r_seg}));
r = r_seg -> second;
segs.erase(r_seg);
}
segs.erase(it);
segs.insert({l, r});
flips.insert({f({l, r}), {l, r}});
}
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++ i) {
cin >> pref[i];
pref[i] += (i > 1 ? pref[i - 2] : 0ll);
flips.insert({f({i, i}), {i, i}});
segs.insert({i, i});
}
pref[n + 1] = pref[n - 1];
for(int t = (n + 1) / 2; t --> 0;) {
auto [l, r] = flips.rbegin()->second;
//cout << l << ' ' << r << ' ' << flips.rbegin()->first << '\n';
ans += flips.rbegin()->first;
flip(l, r);
/**
for(auto &[val, t] : flips) {
cout << val << ' ' << t.first << ' ' << t.second << '\n';
}
// */
cout << ans << '\n';
}
}
Compilation message
candies.cpp:52:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
52 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
672 KB |
Output is correct |
3 |
Correct |
3 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
536 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
672 KB |
Output is correct |
3 |
Correct |
3 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
536 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
375 ms |
27248 KB |
Output is correct |
22 |
Correct |
371 ms |
27204 KB |
Output is correct |
23 |
Correct |
513 ms |
27208 KB |
Output is correct |
24 |
Correct |
161 ms |
27004 KB |
Output is correct |
25 |
Correct |
209 ms |
26992 KB |
Output is correct |
26 |
Correct |
175 ms |
26944 KB |
Output is correct |
27 |
Correct |
187 ms |
27192 KB |
Output is correct |
28 |
Correct |
188 ms |
27204 KB |
Output is correct |
29 |
Correct |
188 ms |
27200 KB |
Output is correct |
30 |
Correct |
188 ms |
27128 KB |
Output is correct |
31 |
Correct |
275 ms |
27228 KB |
Output is correct |
32 |
Correct |
198 ms |
27176 KB |
Output is correct |
33 |
Correct |
291 ms |
27000 KB |
Output is correct |
34 |
Correct |
299 ms |
26996 KB |
Output is correct |
35 |
Correct |
339 ms |
26956 KB |
Output is correct |
36 |
Correct |
393 ms |
27180 KB |
Output is correct |
37 |
Correct |
355 ms |
27212 KB |
Output is correct |
38 |
Correct |
379 ms |
27164 KB |
Output is correct |
39 |
Correct |
176 ms |
27024 KB |
Output is correct |
40 |
Correct |
187 ms |
26960 KB |
Output is correct |
41 |
Correct |
196 ms |
26976 KB |
Output is correct |
42 |
Correct |
200 ms |
27196 KB |
Output is correct |
43 |
Correct |
195 ms |
27192 KB |
Output is correct |
44 |
Correct |
184 ms |
27232 KB |
Output is correct |
45 |
Correct |
220 ms |
27236 KB |
Output is correct |
46 |
Correct |
204 ms |
27120 KB |
Output is correct |
47 |
Correct |
221 ms |
27220 KB |
Output is correct |
48 |
Correct |
272 ms |
26916 KB |
Output is correct |
49 |
Correct |
272 ms |
27040 KB |
Output is correct |
50 |
Correct |
265 ms |
26976 KB |
Output is correct |