답안 #1106019

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106019 2024-10-28T21:34:15 Z LaviniaTornaghi Fireworks (APIO16_fireworks) C++14
100 / 100
143 ms 75704 KB
#include <bits/stdc++.h>
using namespace std;

#define int i64

typedef long long i64;

signed main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  int N, M; cin >> N >> M;

  vector<vector<int>> adj(N);
  vector<int> t(N + M);

  for (int i = 1; i < N + M; i++) {
    int p; cin >> p >> t[i];
    adj[p - 1].push_back(i);
  }

  auto dfs = [&](auto &&dfs, int node) {
    priority_queue<i64> q;
    if (node >= N) {
      q.push(t[node]);
      q.push(t[node]);
      return q;
    }

    for (auto child: adj[node]) {
      priority_queue<i64> cq = dfs(dfs, child);
      
      if (q.size() < cq.size())
      	swap(q, cq);
      while (!cq.empty()) {
        q.push(cq.top());
        cq.pop();
      }
    }
    for (int i = 0; i < adj[node].size() - 1; i++) q.pop();

    i64 x1 = q.top(); q.pop();
    i64 x0 = q.top(); q.pop();

    q.push(x0 + t[node]);
    q.push(x1 + t[node]);

    return q;
  };

  priority_queue<i64> q = dfs(dfs, 0);
  vector<i64> slope_changes;
  while (!q.empty()) {
    int x = q.top(); q.pop();
    slope_changes.push_back(x);
  }

  reverse(slope_changes.begin(), slope_changes.end());
  int curr_slope = 1 - (int)slope_changes.size();
  i64 last_point = 0;
  i64 curr_value = accumulate(t.begin(), t.end(), 0LL);
  i64 best_value = curr_value;

  for (auto x: slope_changes) {
    curr_value += curr_slope * (x - last_point);
    best_value = min(best_value, curr_value);

    last_point = x;
    curr_slope++;
  }

  cout << best_value << '\n';
}

Compilation message

fireworks.cpp: In instantiation of 'main()::<lambda(auto:1&&, i64)> [with auto:1 = main()::<lambda(auto:1&&, i64)>&; i64 = long long int]':
fireworks.cpp:49:37:   required from here
fireworks.cpp:38:23: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int i = 0; i < adj[node].size() - 1; i++) q.pop();
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 472 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 472 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 504 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 2 ms 592 KB Output is correct
34 Correct 2 ms 592 KB Output is correct
35 Correct 3 ms 592 KB Output is correct
36 Correct 3 ms 592 KB Output is correct
37 Correct 3 ms 592 KB Output is correct
38 Correct 2 ms 592 KB Output is correct
39 Correct 2 ms 592 KB Output is correct
40 Correct 3 ms 1616 KB Output is correct
41 Correct 3 ms 1516 KB Output is correct
42 Correct 2 ms 848 KB Output is correct
43 Correct 3 ms 1104 KB Output is correct
44 Correct 4 ms 1104 KB Output is correct
45 Correct 3 ms 848 KB Output is correct
46 Correct 3 ms 768 KB Output is correct
47 Correct 3 ms 592 KB Output is correct
48 Correct 3 ms 592 KB Output is correct
49 Correct 3 ms 592 KB Output is correct
50 Correct 2 ms 592 KB Output is correct
51 Correct 2 ms 592 KB Output is correct
52 Correct 2 ms 592 KB Output is correct
53 Correct 2 ms 592 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 472 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 504 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 2 ms 592 KB Output is correct
34 Correct 2 ms 592 KB Output is correct
35 Correct 3 ms 592 KB Output is correct
36 Correct 3 ms 592 KB Output is correct
37 Correct 3 ms 592 KB Output is correct
38 Correct 2 ms 592 KB Output is correct
39 Correct 2 ms 592 KB Output is correct
40 Correct 3 ms 1616 KB Output is correct
41 Correct 3 ms 1516 KB Output is correct
42 Correct 2 ms 848 KB Output is correct
43 Correct 3 ms 1104 KB Output is correct
44 Correct 4 ms 1104 KB Output is correct
45 Correct 3 ms 848 KB Output is correct
46 Correct 3 ms 768 KB Output is correct
47 Correct 3 ms 592 KB Output is correct
48 Correct 3 ms 592 KB Output is correct
49 Correct 3 ms 592 KB Output is correct
50 Correct 2 ms 592 KB Output is correct
51 Correct 2 ms 592 KB Output is correct
52 Correct 2 ms 592 KB Output is correct
53 Correct 2 ms 592 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 5 ms 1360 KB Output is correct
56 Correct 18 ms 3784 KB Output is correct
57 Correct 42 ms 6080 KB Output is correct
58 Correct 39 ms 7852 KB Output is correct
59 Correct 52 ms 10292 KB Output is correct
60 Correct 66 ms 12432 KB Output is correct
61 Correct 80 ms 14604 KB Output is correct
62 Correct 85 ms 15880 KB Output is correct
63 Correct 110 ms 18716 KB Output is correct
64 Correct 118 ms 19752 KB Output is correct
65 Correct 121 ms 75704 KB Output is correct
66 Correct 123 ms 75568 KB Output is correct
67 Correct 70 ms 24132 KB Output is correct
68 Correct 143 ms 47552 KB Output is correct
69 Correct 143 ms 43960 KB Output is correct
70 Correct 131 ms 43964 KB Output is correct
71 Correct 130 ms 22772 KB Output is correct
72 Correct 129 ms 22888 KB Output is correct
73 Correct 120 ms 22204 KB Output is correct
74 Correct 122 ms 22200 KB Output is correct
75 Correct 128 ms 21528 KB Output is correct
76 Correct 129 ms 21644 KB Output is correct
77 Correct 128 ms 21676 KB Output is correct
78 Correct 126 ms 21272 KB Output is correct
79 Correct 123 ms 24364 KB Output is correct