답안 #946400

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
946400 2024-03-14T15:55:39 Z Sokol080808 Candies (JOI18_candies) C++17
100 / 100
449 ms 28032 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using ll = long long;
using ull = unsigned long long;
using ld = long double;

const ll INF = 1e18;

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

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

    int mxc = (n + 1) / 2;
    
    set<array<ll, 3>> segs;
    set<array<ll, 3>> best;
    for (int i = 1; i <= n; i++) {
        segs.insert({i, i, a[i]});
        best.insert({a[i], i, i});
    }

    ll ans = 0;
    for (int iteration = 0; iteration < mxc; iteration++) {
        auto it = prev(best.end());
        auto [add, l, r] = *it;
        segs.erase({l, r, add});
        best.erase({add, l, r});
        ans += add;
        cout << ans << '\n';

        if (best.empty()) break;

        auto it2 = segs.lower_bound({l, INF, INF});
        if (it2 == segs.end()) {
            it2 = prev(it2);
            best.erase({(*it2)[2], (*it2)[0], (*it2)[1]});
            segs.erase(it2);
            continue;
        }
        if (it2 == segs.begin()) {
            best.erase({(*it2)[2], (*it2)[0], (*it2)[1]});
            segs.erase(it2);
            continue;
        }
        
        add = -add;
        auto nxt = segs.lower_bound({l, INF, INF});
        if (nxt != segs.end()) {
            add += (*nxt)[2];
            r = (*nxt)[1];
            best.erase({(*nxt)[2], (*nxt)[0], (*nxt)[1]});
            segs.erase(nxt);
        }
        auto prv = segs.lower_bound({l, INF, INF});
        if (prv != segs.begin()) {
            prv = prev(prv);
            add += (*prv)[2];
            l = (*prv)[0];
            best.erase({(*prv)[2], (*prv)[0], (*prv)[1]});
            segs.erase(prv);
        }

        segs.insert({l, r, add});
        best.insert({add, l, r});
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 728 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 856 KB Output is correct
14 Correct 2 ms 624 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 728 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 856 KB Output is correct
14 Correct 2 ms 624 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 449 ms 27696 KB Output is correct
22 Correct 436 ms 27768 KB Output is correct
23 Correct 425 ms 27988 KB Output is correct
24 Correct 210 ms 27536 KB Output is correct
25 Correct 248 ms 27868 KB Output is correct
26 Correct 209 ms 27728 KB Output is correct
27 Correct 257 ms 27728 KB Output is correct
28 Correct 221 ms 27724 KB Output is correct
29 Correct 244 ms 27532 KB Output is correct
30 Correct 249 ms 27652 KB Output is correct
31 Correct 245 ms 27584 KB Output is correct
32 Correct 234 ms 27732 KB Output is correct
33 Correct 316 ms 27728 KB Output is correct
34 Correct 325 ms 27680 KB Output is correct
35 Correct 321 ms 27620 KB Output is correct
36 Correct 435 ms 28032 KB Output is correct
37 Correct 447 ms 27728 KB Output is correct
38 Correct 440 ms 27692 KB Output is correct
39 Correct 211 ms 27616 KB Output is correct
40 Correct 225 ms 27984 KB Output is correct
41 Correct 211 ms 27732 KB Output is correct
42 Correct 225 ms 27728 KB Output is correct
43 Correct 229 ms 27644 KB Output is correct
44 Correct 227 ms 27732 KB Output is correct
45 Correct 244 ms 27720 KB Output is correct
46 Correct 235 ms 27764 KB Output is correct
47 Correct 238 ms 27728 KB Output is correct
48 Correct 306 ms 27724 KB Output is correct
49 Correct 310 ms 27524 KB Output is correct
50 Correct 312 ms 27728 KB Output is correct