답안 #42424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42424 2018-02-27T05:50:26 Z choikiwon 케이크 (JOI13_cake) C++14
100 / 100
469 ms 160908 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

int N;
int A[300010], B[300010];
ll psum[2][300010], ans[300010];

ll calc(int t, int l, int r) {
    return psum[t][r] - (l == 0? 0 : psum[t][l - 1]);
}

struct BIT {
    vector<pii> tree;
    void init() {
        tree = vector<pii>(4*N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n] = pii(B[l], l);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = min(tree[2*n], tree[2*n + 1]);
    }
    pii quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return pii(1e9, 1e9);
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        pii L = quer(a, b, l, m, 2*n);
        pii R = quer(a, b, m + 1, r, 2*n + 1);
        return min(L, R);
    }
} bit;

void solve(int l, int r, ll add) {
    if(l > r) return;

    pii t = bit.quer(l, r, 0, N - 1, 1);
    solve(l, t.second - 1, add + ((t.second - l) % 2? calc((t.second % 2) ^ 1, t.second, r) : calc(t.second % 2, t.second, r)));
    solve(t.second + 1, r, add + ((r - t.second) % 2? calc((t.second % 2) ^ 1, l, t.second) : calc(t.second % 2, l, t.second)));

    //cout << "solve : " << l << ' ' << r << ' ' << t.second << ' ' << add << endl;

    ans[t.second] = add + B[t.second];
    while(l < r) {
        //cout << l << ' ' << r << ' ' << ans[t.second] << endl;

        pii t1 = bit.quer(l, t.second - 1, 0, N - 1, 1);
        pii t2 = bit.quer(t.second + 1, r, 0, N - 1, 1);
        if(t1 < t2) {
            ans[t.second] += (r - t1.second) % 2? calc((t1.second % 2) ^ 1, l, t1.second) : calc(t1.second % 2, l, t1.second);
            l = t1.second + 1;
        }
        else {
            ans[t.second] += (t2.second - l) % 2? calc((t2.second % 2) ^ 1, t2.second, r) : calc(t2.second % 2, t2.second, r);
            r = t2.second - 1;
        }
    }
}

int main() {
    scanf("%d", &N);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
    }

    int p = 0;
    for(int i = 0; i < N; i++) {
        if(A[p] > A[i]) p = i;
    }
    for(int i = 0; i < N; i++) {
        B[i] = A[(i + p) % N];
    }
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < 2; j++) {
            psum[j][i] = i % 2 == j? B[i] : 0;
            if(i) psum[j][i] += psum[j][i - 1];
        }
    }

    int l = 1, r = N - 1, t = 0;
    ans[0] = B[0];
    while(l <= r) {
        if(B[l] >= B[r]) {
            if(t) ans[0] += B[l];
            l++;
        }
        else {
            if(t) ans[0] += B[r];
            r--;
        }
        t ^= 1;
    }

    bit.init();
    solve(1, N - 1, N % 2? B[0] : 0);

    for(int i = 0; i < N; i++) {
        printf("%lld\n", ans[ (i + N - p) % N ]);
    }
}

Compilation message

cake.cpp: In function 'int main()':
cake.cpp:68:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
                    ^
cake.cpp:71:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &A[i]);
                           ^
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 760 KB Output is correct
2 Correct 6 ms 2724 KB Output is correct
3 Correct 8 ms 2724 KB Output is correct
4 Correct 11 ms 2724 KB Output is correct
5 Correct 10 ms 2724 KB Output is correct
6 Correct 7 ms 2724 KB Output is correct
7 Correct 7 ms 2724 KB Output is correct
8 Correct 12 ms 2724 KB Output is correct
9 Correct 2 ms 2724 KB Output is correct
10 Correct 2 ms 2724 KB Output is correct
11 Correct 2 ms 2724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 10904 KB Output is correct
2 Correct 137 ms 11784 KB Output is correct
3 Correct 139 ms 13436 KB Output is correct
4 Correct 307 ms 24268 KB Output is correct
5 Correct 263 ms 24268 KB Output is correct
6 Correct 407 ms 34196 KB Output is correct
7 Correct 453 ms 71416 KB Output is correct
8 Correct 404 ms 71416 KB Output is correct
9 Correct 414 ms 71416 KB Output is correct
10 Correct 444 ms 97324 KB Output is correct
11 Correct 411 ms 97324 KB Output is correct
12 Correct 416 ms 97324 KB Output is correct
13 Correct 469 ms 100036 KB Output is correct
14 Correct 351 ms 160908 KB Output is correct
15 Correct 409 ms 160908 KB Output is correct