답안 #45002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45002 2018-04-10T12:22:57 Z bogdan10bos Candies (JOI18_candies) C++14
100 / 100
688 ms 20652 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});
        }
        else
        {
            prv[ nxt[pos] ] = prv[pos];
            nxt[ prv[pos] ] = nxt[pos];
        }
    }

    return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
candies.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &v[i]);
         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 4 ms 612 KB Output is correct
3 Correct 3 ms 724 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 3 ms 852 KB Output is correct
8 Correct 4 ms 904 KB Output is correct
9 Correct 5 ms 944 KB Output is correct
10 Correct 4 ms 944 KB Output is correct
11 Correct 4 ms 944 KB Output is correct
12 Correct 3 ms 944 KB Output is correct
13 Correct 3 ms 944 KB Output is correct
14 Correct 3 ms 944 KB Output is correct
15 Correct 3 ms 1052 KB Output is correct
16 Correct 3 ms 1068 KB Output is correct
17 Correct 3 ms 1068 KB Output is correct
18 Correct 3 ms 1068 KB Output is correct
19 Correct 3 ms 1068 KB Output is correct
20 Correct 3 ms 1068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 4 ms 612 KB Output is correct
3 Correct 3 ms 724 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 3 ms 852 KB Output is correct
8 Correct 4 ms 904 KB Output is correct
9 Correct 5 ms 944 KB Output is correct
10 Correct 4 ms 944 KB Output is correct
11 Correct 4 ms 944 KB Output is correct
12 Correct 3 ms 944 KB Output is correct
13 Correct 3 ms 944 KB Output is correct
14 Correct 3 ms 944 KB Output is correct
15 Correct 3 ms 1052 KB Output is correct
16 Correct 3 ms 1068 KB Output is correct
17 Correct 3 ms 1068 KB Output is correct
18 Correct 3 ms 1068 KB Output is correct
19 Correct 3 ms 1068 KB Output is correct
20 Correct 3 ms 1068 KB Output is correct
21 Correct 649 ms 20468 KB Output is correct
22 Correct 688 ms 20652 KB Output is correct
23 Correct 636 ms 20652 KB Output is correct
24 Correct 245 ms 20652 KB Output is correct
25 Correct 210 ms 20652 KB Output is correct
26 Correct 203 ms 20652 KB Output is correct
27 Correct 212 ms 20652 KB Output is correct
28 Correct 210 ms 20652 KB Output is correct
29 Correct 209 ms 20652 KB Output is correct
30 Correct 224 ms 20652 KB Output is correct
31 Correct 217 ms 20652 KB Output is correct
32 Correct 210 ms 20652 KB Output is correct
33 Correct 334 ms 20652 KB Output is correct
34 Correct 312 ms 20652 KB Output is correct
35 Correct 393 ms 20652 KB Output is correct
36 Correct 547 ms 20652 KB Output is correct
37 Correct 470 ms 20652 KB Output is correct
38 Correct 556 ms 20652 KB Output is correct
39 Correct 201 ms 20652 KB Output is correct
40 Correct 248 ms 20652 KB Output is correct
41 Correct 241 ms 20652 KB Output is correct
42 Correct 244 ms 20652 KB Output is correct
43 Correct 252 ms 20652 KB Output is correct
44 Correct 246 ms 20652 KB Output is correct
45 Correct 222 ms 20652 KB Output is correct
46 Correct 280 ms 20652 KB Output is correct
47 Correct 228 ms 20652 KB Output is correct
48 Correct 370 ms 20652 KB Output is correct
49 Correct 377 ms 20652 KB Output is correct
50 Correct 357 ms 20652 KB Output is correct