답안 #728104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
728104 2023-04-22T02:14:33 Z josanneo22 Fireworks (APIO16_fireworks) C++17
100 / 100
318 ms 74420 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <functional>
#include <numeric>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <map>
#include <set>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const lint inf = 1e16;

int n, m, sz[300005];
lint dep[300005], c[300005];

vector<int> gph[300005];

struct func {
    priority_queue<lint> pq;
    lint cost;
    int slope;
    void init() {
        cost = 0;
        slope = -1;
        pq.push(0);
        pq.push(0);
    }
    void upperize(int x) {
        cost += c[x];
        while (!pq.empty() && slope + (int)pq.size() > 1) {
            pq.pop();
        }
        vector<lint> v;
        while (!pq.empty() && slope + (int)pq.size() >= 0) {
            v.push_back(pq.top());
            pq.pop();
        }
        while (!v.empty()) {
            pq.push(v.back() + c[x]);
            v.pop_back();
        }
    }
}dp[300005];

bool cmp(int a, int b) {
    return sz[a] > sz[b];
}

void dfs(int x) {
    if (x > n) {
        sz[x] = 1;
        return;
    }
    for (int i = 0; i < gph[x].size(); i++) {
        dep[gph[x][i]] = dep[x] + c[gph[x][i]];
        dfs(gph[x][i]);
        sz[x] += sz[gph[x][i]];
    }
    sort(gph[x].begin(), gph[x].end(), cmp);
}

int solve(int x) {
    if (x > n) {
        dp[x].init();
        return x;
    }
    int ret = solve(gph[x][0]);
    dp[ret].upperize(gph[x][0]);
    for (int i = 1; i < gph[x].size(); i++) {
        int t = solve(gph[x][i]);
        dp[t].upperize(gph[x][i]);
        dp[ret].cost += dp[t].cost;
        dp[ret].slope += dp[t].slope;
        while (!dp[t].pq.empty()) {
            dp[ret].pq.push(dp[t].pq.top());
            dp[t].pq.pop();
        }
    }
    return ret;
}

int main() {
    cin >> n >> m;
    for (int i = 2; i <= n + m; i++) {
        int p;
        scanf("%d %lld", &p, &c[i]);
        gph[p].push_back(i);
    }
    dfs(1);
    func ret = dp[solve(1)];
    ret.upperize(0);
    lint ansp = ret.pq.top();
    lint ans = ret.cost + ansp * ret.slope;
    while (!ret.pq.empty()) {
        ans += ansp - ret.pq.top();
        ret.pq.pop();
    }
    printf("%lld", ans);
}

Compilation message

fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for (int i = 0; i < gph[x].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
fireworks.cpp: In function 'int solve(int)':
fireworks.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 1; i < gph[x].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         scanf("%d %lld", &p, &c[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 21448 KB Output is correct
2 Correct 11 ms 21444 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 11 ms 21452 KB Output is correct
5 Correct 11 ms 21472 KB Output is correct
6 Correct 11 ms 21460 KB Output is correct
7 Correct 11 ms 21460 KB Output is correct
8 Correct 12 ms 21460 KB Output is correct
9 Correct 11 ms 21428 KB Output is correct
10 Correct 11 ms 21360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 21440 KB Output is correct
2 Correct 11 ms 21368 KB Output is correct
3 Correct 12 ms 21448 KB Output is correct
4 Correct 13 ms 21460 KB Output is correct
5 Correct 13 ms 21460 KB Output is correct
6 Correct 11 ms 21460 KB Output is correct
7 Correct 11 ms 21460 KB Output is correct
8 Correct 12 ms 21444 KB Output is correct
9 Correct 14 ms 21444 KB Output is correct
10 Correct 11 ms 21480 KB Output is correct
11 Correct 12 ms 21460 KB Output is correct
12 Correct 12 ms 21444 KB Output is correct
13 Correct 14 ms 21388 KB Output is correct
14 Correct 13 ms 21460 KB Output is correct
15 Correct 13 ms 21460 KB Output is correct
16 Correct 13 ms 21408 KB Output is correct
17 Correct 11 ms 21460 KB Output is correct
18 Correct 10 ms 21460 KB Output is correct
19 Correct 12 ms 21452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 21448 KB Output is correct
2 Correct 11 ms 21444 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 11 ms 21452 KB Output is correct
5 Correct 11 ms 21472 KB Output is correct
6 Correct 11 ms 21460 KB Output is correct
7 Correct 11 ms 21460 KB Output is correct
8 Correct 12 ms 21460 KB Output is correct
9 Correct 11 ms 21428 KB Output is correct
10 Correct 11 ms 21360 KB Output is correct
11 Correct 13 ms 21440 KB Output is correct
12 Correct 11 ms 21368 KB Output is correct
13 Correct 12 ms 21448 KB Output is correct
14 Correct 13 ms 21460 KB Output is correct
15 Correct 13 ms 21460 KB Output is correct
16 Correct 11 ms 21460 KB Output is correct
17 Correct 11 ms 21460 KB Output is correct
18 Correct 12 ms 21444 KB Output is correct
19 Correct 14 ms 21444 KB Output is correct
20 Correct 11 ms 21480 KB Output is correct
21 Correct 12 ms 21460 KB Output is correct
22 Correct 12 ms 21444 KB Output is correct
23 Correct 14 ms 21388 KB Output is correct
24 Correct 13 ms 21460 KB Output is correct
25 Correct 13 ms 21460 KB Output is correct
26 Correct 13 ms 21408 KB Output is correct
27 Correct 11 ms 21460 KB Output is correct
28 Correct 10 ms 21460 KB Output is correct
29 Correct 12 ms 21452 KB Output is correct
30 Correct 14 ms 21460 KB Output is correct
31 Correct 16 ms 21464 KB Output is correct
32 Correct 12 ms 21588 KB Output is correct
33 Correct 14 ms 21572 KB Output is correct
34 Correct 12 ms 21588 KB Output is correct
35 Correct 16 ms 21736 KB Output is correct
36 Correct 16 ms 21748 KB Output is correct
37 Correct 14 ms 21840 KB Output is correct
38 Correct 16 ms 21852 KB Output is correct
39 Correct 16 ms 21760 KB Output is correct
40 Correct 16 ms 22228 KB Output is correct
41 Correct 18 ms 22204 KB Output is correct
42 Correct 20 ms 21732 KB Output is correct
43 Correct 15 ms 22100 KB Output is correct
44 Correct 16 ms 21972 KB Output is correct
45 Correct 15 ms 21964 KB Output is correct
46 Correct 15 ms 21928 KB Output is correct
47 Correct 17 ms 21908 KB Output is correct
48 Correct 16 ms 21844 KB Output is correct
49 Correct 14 ms 21960 KB Output is correct
50 Correct 15 ms 21844 KB Output is correct
51 Correct 14 ms 21844 KB Output is correct
52 Correct 14 ms 21772 KB Output is correct
53 Correct 17 ms 21840 KB Output is correct
54 Correct 17 ms 21972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 21448 KB Output is correct
2 Correct 11 ms 21444 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 11 ms 21452 KB Output is correct
5 Correct 11 ms 21472 KB Output is correct
6 Correct 11 ms 21460 KB Output is correct
7 Correct 11 ms 21460 KB Output is correct
8 Correct 12 ms 21460 KB Output is correct
9 Correct 11 ms 21428 KB Output is correct
10 Correct 11 ms 21360 KB Output is correct
11 Correct 13 ms 21440 KB Output is correct
12 Correct 11 ms 21368 KB Output is correct
13 Correct 12 ms 21448 KB Output is correct
14 Correct 13 ms 21460 KB Output is correct
15 Correct 13 ms 21460 KB Output is correct
16 Correct 11 ms 21460 KB Output is correct
17 Correct 11 ms 21460 KB Output is correct
18 Correct 12 ms 21444 KB Output is correct
19 Correct 14 ms 21444 KB Output is correct
20 Correct 11 ms 21480 KB Output is correct
21 Correct 12 ms 21460 KB Output is correct
22 Correct 12 ms 21444 KB Output is correct
23 Correct 14 ms 21388 KB Output is correct
24 Correct 13 ms 21460 KB Output is correct
25 Correct 13 ms 21460 KB Output is correct
26 Correct 13 ms 21408 KB Output is correct
27 Correct 11 ms 21460 KB Output is correct
28 Correct 10 ms 21460 KB Output is correct
29 Correct 12 ms 21452 KB Output is correct
30 Correct 14 ms 21460 KB Output is correct
31 Correct 16 ms 21464 KB Output is correct
32 Correct 12 ms 21588 KB Output is correct
33 Correct 14 ms 21572 KB Output is correct
34 Correct 12 ms 21588 KB Output is correct
35 Correct 16 ms 21736 KB Output is correct
36 Correct 16 ms 21748 KB Output is correct
37 Correct 14 ms 21840 KB Output is correct
38 Correct 16 ms 21852 KB Output is correct
39 Correct 16 ms 21760 KB Output is correct
40 Correct 16 ms 22228 KB Output is correct
41 Correct 18 ms 22204 KB Output is correct
42 Correct 20 ms 21732 KB Output is correct
43 Correct 15 ms 22100 KB Output is correct
44 Correct 16 ms 21972 KB Output is correct
45 Correct 15 ms 21964 KB Output is correct
46 Correct 15 ms 21928 KB Output is correct
47 Correct 17 ms 21908 KB Output is correct
48 Correct 16 ms 21844 KB Output is correct
49 Correct 14 ms 21960 KB Output is correct
50 Correct 15 ms 21844 KB Output is correct
51 Correct 14 ms 21844 KB Output is correct
52 Correct 14 ms 21772 KB Output is correct
53 Correct 17 ms 21840 KB Output is correct
54 Correct 17 ms 21972 KB Output is correct
55 Correct 22 ms 22484 KB Output is correct
56 Correct 70 ms 25520 KB Output is correct
57 Correct 98 ms 28492 KB Output is correct
58 Correct 112 ms 30432 KB Output is correct
59 Correct 168 ms 33576 KB Output is correct
60 Correct 219 ms 36552 KB Output is correct
61 Correct 225 ms 38520 KB Output is correct
62 Correct 244 ms 40488 KB Output is correct
63 Correct 304 ms 44340 KB Output is correct
64 Correct 318 ms 45288 KB Output is correct
65 Correct 208 ms 74420 KB Output is correct
66 Correct 179 ms 74404 KB Output is correct
67 Correct 195 ms 41664 KB Output is correct
68 Correct 229 ms 60212 KB Output is correct
69 Correct 250 ms 57412 KB Output is correct
70 Correct 277 ms 57548 KB Output is correct
71 Correct 237 ms 56168 KB Output is correct
72 Correct 255 ms 56128 KB Output is correct
73 Correct 283 ms 52784 KB Output is correct
74 Correct 274 ms 53044 KB Output is correct
75 Correct 265 ms 51856 KB Output is correct
76 Correct 235 ms 51900 KB Output is correct
77 Correct 256 ms 50548 KB Output is correct
78 Correct 278 ms 49504 KB Output is correct
79 Correct 226 ms 50856 KB Output is correct