# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45001 | 2018-04-10T12:20:31 Z | bogdan10bos | Candies (JOI18_candies) | C++14 | 4 ms | 504 KB |
#include <bits/stdc++.h> using namespace std; //#define FILE_IO typedef long long LL; typedef pair<LL, int> pii; LL sum[200005]; int N; int v[200005]; int nxt[200005], prv[200005]; int st[200005], dr[200005]; set<pii> s; int main() { #ifdef FILE_IO freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); #endif scanf("%d", &N); for(int i = 1; i <= N; i++) { scanf("%d", &v[i]); sum[i] = sum[i - 1] + ( (i & 1) ? v[i] : -v[i] ); nxt[i] = i + 1; prv[i] = i - 1; st[i] = dr[i] = i; s.insert({v[i], i}); } LL ans = 0; for(int i = 1; i <= (N + 1) / 2; i++) { auto it = s.end(); it--; LL val = (*it).first; int pos = (*it).second; s.erase(it); ans += val; printf("%lld\n", ans); if(i == (N + 1) / 2) break; int p = prv[pos]; int u = nxt[pos]; int cnt = 0; if(p != 0) { cnt++; LL val = sum[ dr[p] ] - sum[ st[p] - 1 ]; if(st[p] % 2 == 0) val = -val; s.erase({val, p}); nxt[ prv[p] ] = pos; prv[pos] = prv[p]; st[pos] = st[p]; } if(u != N + 1) { cnt++; LL val = sum[ dr[u] ] - sum[ st[u] - 1 ]; if(st[u] % 2 == 0) val = -val; s.erase({val, u}); prv[ nxt[u] ] = pos; nxt[pos] = nxt[u]; dr[pos] = dr[u]; } if(cnt == 2) { LL val = sum[ dr[pos] ] - sum[ st[pos] - 1 ]; if(st[pos] % 2 == 0) val = -val; s.insert({val, pos}); } } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 4 ms | 504 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 4 ms | 504 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |