답안 #197867

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
197867 2020-01-23T18:44:26 Z dolphingarlic Candies (JOI18_candies) C++14
100 / 100
381 ms 19888 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Edge {
    int l, r;
    ll cost;
};
bool operator<(Edge A, Edge B) { return A.l < B.l; }
bool operator>(Edge A, Edge B) { return A.cost < B.cost; }

Edge merge(Edge A, Edge B, Edge C) { return {A.l, C.r, A.cost + C.cost - B.cost}; }

bool visited[200001];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n;

    priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
    set<Edge> edges;
    for (int i = 1; i <= n; i++) {
        ll a;
        cin >> a;
        Edge e = {i, i + 1, a};
        pq.push(e);
        edges.insert(e);
    }
    Edge e = {n + 1, n + 2, (ll)-1e14};
    pq.push(e);
    edges.insert(e);
    e = {0, 1, (ll)-1e14};
    pq.push(e);
    edges.insert(e);

    ll ans = 0;
    for (int i = 0; i < (n + 1) / 2;) {
        Edge curr = pq.top();
        pq.pop();
        if (!visited[curr.l] && !visited[curr.r]) {
            i++;
            ans += curr.cost;
            visited[curr.l] = visited[curr.r] = true;

            auto it = edges.find(curr);
            Edge l = *prev(it), r = *next(it);
            Edge newedge = merge(l, curr, r);
            edges.erase(l); edges.erase(curr); edges.erase(r);
            edges.insert(newedge);
            pq.push(newedge);

            cout << ans << '\n';
        }
    }
    return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:19:12: warning: unused variable 'k' [-Wunused-variable]
     int n, k;
            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 504 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 4 ms 376 KB Output is correct
6 Correct 4 ms 504 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 4 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 4 ms 504 KB Output is correct
11 Correct 4 ms 552 KB Output is correct
12 Correct 4 ms 504 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
14 Correct 4 ms 504 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 4 ms 504 KB Output is correct
17 Correct 4 ms 504 KB Output is correct
18 Correct 4 ms 504 KB Output is correct
19 Correct 4 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 504 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 4 ms 376 KB Output is correct
6 Correct 4 ms 504 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 4 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 4 ms 504 KB Output is correct
11 Correct 4 ms 552 KB Output is correct
12 Correct 4 ms 504 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
14 Correct 4 ms 504 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 4 ms 504 KB Output is correct
17 Correct 4 ms 504 KB Output is correct
18 Correct 4 ms 504 KB Output is correct
19 Correct 4 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 377 ms 17724 KB Output is correct
22 Correct 374 ms 19764 KB Output is correct
23 Correct 381 ms 19804 KB Output is correct
24 Correct 263 ms 19676 KB Output is correct
25 Correct 262 ms 19484 KB Output is correct
26 Correct 260 ms 19548 KB Output is correct
27 Correct 274 ms 19676 KB Output is correct
28 Correct 277 ms 19756 KB Output is correct
29 Correct 279 ms 19804 KB Output is correct
30 Correct 283 ms 19712 KB Output is correct
31 Correct 287 ms 19676 KB Output is correct
32 Correct 285 ms 19816 KB Output is correct
33 Correct 313 ms 19548 KB Output is correct
34 Correct 312 ms 19420 KB Output is correct
35 Correct 317 ms 19772 KB Output is correct
36 Correct 373 ms 19804 KB Output is correct
37 Correct 372 ms 19724 KB Output is correct
38 Correct 367 ms 19676 KB Output is correct
39 Correct 259 ms 19484 KB Output is correct
40 Correct 262 ms 19636 KB Output is correct
41 Correct 264 ms 19732 KB Output is correct
42 Correct 275 ms 19676 KB Output is correct
43 Correct 274 ms 19680 KB Output is correct
44 Correct 305 ms 19704 KB Output is correct
45 Correct 284 ms 19804 KB Output is correct
46 Correct 291 ms 19756 KB Output is correct
47 Correct 285 ms 19888 KB Output is correct
48 Correct 316 ms 19548 KB Output is correct
49 Correct 315 ms 19496 KB Output is correct
50 Correct 316 ms 19548 KB Output is correct