# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45001 | bogdan10bos | Candies (JOI18_candies) | C++14 | 4 ms | 504 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |