답안 #234042

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
234042 2020-05-22T22:09:56 Z DS007 Candies (JOI18_candies) C++14
100 / 100
1082 ms 58748 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

int solveTestCase(int test) {
    int n;
    cin >> n;

    int a[n + 2] = {};
    for (int i = 1; i <= n; i++) cin >> a[i];

    int p1[n + 2] = {}, p2[n + 2] = {};
    p1[1] = a[1];
    for (int i = 2; i <= n; i++) {
        if (i % 2)
            p1[i] = a[i] + p1[i - 2];
        else
            p2[i] = a[i] + p2[i - 2];
    }

    set<pair<int, pair<int, int>>, decltype(greater<>())> s;
    map<int, int> begin, end;
    map<pair<int, int>, int> rev;
    for (int i = 1; i <= n; i++) {
        s.insert(make_pair(a[i], make_pair(i, i)));
        begin[i] = i;
        end[i] = i;
        rev[{i, i}] = a[i];
    }

    int ans = 0;
    for (int i = 1; i <= (n + 1) / 2; i++) {
        auto temp = s.begin();
        bool check = true;
        ans += temp->first;
        cout << ans << "\n";

        pair<int, int> next = temp->second;
        end.erase(next.second);
        begin.erase(next.first);
        rev.erase(next);

        auto it1 = begin.find(next.second + 1);
        if (it1 != begin.end()) {
            next.second = it1->second - 1;
            s.erase(make_pair(rev[*it1], *it1));
            rev.erase(*it1);
            end.erase(it1->second);
            begin.erase(it1);
        } else check = false;

        auto it2 = end.find(next.first - 1);
        if (it2 != end.end()) {
            next.first = it2->second + 1;
            s.erase(make_pair(rev[{it2->second, it2->first}], make_pair(it2->second, it2->first)));
            rev.erase({it2->second, it2->first});
            begin.erase(it2->second);
            end.erase(it2);
        } else check = false;

        int off = 0;
        if (next.second == n)
            next.second--, off += a[n];
        else
            next.second++;

        if (next.first == 1)
            next.first++, off += a[1];
        else
            next.first--;

        s.erase(temp);
        //assert((next.second - next.first) % 2 == 0 && next.second - next.first != 0);

        int val = -off;
        if (next.second != next.first && check) {
            val += next.second % 2 ? p1[next.second] - p1[next.first] + a[next.first] - (p2[next.second - 1] - p2[next.first + 1] + a[next.first + 1]) : p2[next.second] - p2[next.first] + a[next.first] - (p1[next.second - 1] - p1[next.first + 1] + a[next.first + 1]);
            s.insert(make_pair(val, next));
            rev[next] = val;
            begin.insert(next);
            end[next.second] = next.first;
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int test = 1;
    //cin >> test;
    for (int i = 1; i <= test; i++)
        solveTestCase(i);
}

Compilation message

candies.cpp: In function 'long long int solveTestCase(long long int)':
candies.cpp:84:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 896 KB Output is correct
2 Correct 9 ms 896 KB Output is correct
3 Correct 8 ms 896 KB Output is correct
4 Correct 8 ms 896 KB Output is correct
5 Correct 9 ms 896 KB Output is correct
6 Correct 8 ms 896 KB Output is correct
7 Correct 10 ms 1024 KB Output is correct
8 Correct 9 ms 896 KB Output is correct
9 Correct 8 ms 896 KB Output is correct
10 Correct 8 ms 896 KB Output is correct
11 Correct 9 ms 896 KB Output is correct
12 Correct 9 ms 896 KB Output is correct
13 Correct 9 ms 896 KB Output is correct
14 Correct 8 ms 896 KB Output is correct
15 Correct 8 ms 896 KB Output is correct
16 Correct 9 ms 896 KB Output is correct
17 Correct 8 ms 896 KB Output is correct
18 Correct 8 ms 896 KB Output is correct
19 Correct 8 ms 896 KB Output is correct
20 Correct 8 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 896 KB Output is correct
2 Correct 9 ms 896 KB Output is correct
3 Correct 8 ms 896 KB Output is correct
4 Correct 8 ms 896 KB Output is correct
5 Correct 9 ms 896 KB Output is correct
6 Correct 8 ms 896 KB Output is correct
7 Correct 10 ms 1024 KB Output is correct
8 Correct 9 ms 896 KB Output is correct
9 Correct 8 ms 896 KB Output is correct
10 Correct 8 ms 896 KB Output is correct
11 Correct 9 ms 896 KB Output is correct
12 Correct 9 ms 896 KB Output is correct
13 Correct 9 ms 896 KB Output is correct
14 Correct 8 ms 896 KB Output is correct
15 Correct 8 ms 896 KB Output is correct
16 Correct 9 ms 896 KB Output is correct
17 Correct 8 ms 896 KB Output is correct
18 Correct 8 ms 896 KB Output is correct
19 Correct 8 ms 896 KB Output is correct
20 Correct 8 ms 896 KB Output is correct
21 Correct 1070 ms 58616 KB Output is correct
22 Correct 1068 ms 58744 KB Output is correct
23 Correct 1036 ms 58636 KB Output is correct
24 Correct 528 ms 58360 KB Output is correct
25 Correct 530 ms 58488 KB Output is correct
26 Correct 521 ms 58492 KB Output is correct
27 Correct 556 ms 58744 KB Output is correct
28 Correct 544 ms 58744 KB Output is correct
29 Correct 554 ms 58744 KB Output is correct
30 Correct 574 ms 58744 KB Output is correct
31 Correct 586 ms 58748 KB Output is correct
32 Correct 573 ms 58620 KB Output is correct
33 Correct 760 ms 58488 KB Output is correct
34 Correct 749 ms 58360 KB Output is correct
35 Correct 791 ms 58616 KB Output is correct
36 Correct 1058 ms 58620 KB Output is correct
37 Correct 1082 ms 58656 KB Output is correct
38 Correct 1027 ms 58744 KB Output is correct
39 Correct 511 ms 58360 KB Output is correct
40 Correct 523 ms 58488 KB Output is correct
41 Correct 517 ms 58492 KB Output is correct
42 Correct 541 ms 58548 KB Output is correct
43 Correct 550 ms 58616 KB Output is correct
44 Correct 553 ms 58616 KB Output is correct
45 Correct 576 ms 58616 KB Output is correct
46 Correct 591 ms 58616 KB Output is correct
47 Correct 573 ms 58616 KB Output is correct
48 Correct 747 ms 58488 KB Output is correct
49 Correct 748 ms 58488 KB Output is correct
50 Correct 760 ms 58360 KB Output is correct