Submission #389316

# Submission time Handle Problem Language Result Execution time Memory
389316 2021-04-14T02:55:24 Z rama_pang Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
724 ms 122576 KB
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int N;
  cin >> N;

  vector<int> A(N), H(N), C(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> H[i] >> C[i];
    A[i]--;
  }

  { // Coordinate Compression
    vector<int> coords;
    for (int i = 0; i < N; i++) {
      coords.emplace_back(H[i]);
    }
    sort(begin(coords), end(coords));
    coords.resize(unique(begin(coords), end(coords)) - begin(coords));
    for (int i = 0; i < N; i++) {
      H[i] = lower_bound(begin(coords), end(coords), H[i]) - begin(coords);
    }
  }

  const auto Solve = [&](vector<int> component) -> long long {
    // Cycle + Tree graph
    // Cycle = super root, has many possible costs, try all of them
    //
    // Solve for tree:
    // dp[u][x] = minimum cost when H[u] >= x
    // diff[u][x] = dp[u][x] - dp[u][x - 1]
    //
    // Note:
    // dp[u][x] <= dp[u][x + 1], since dp[u][x] = min(dp[u][x], dp[u][x + 1])
    // 0 <= diff[x + 1]
    //
    // Case 1: Don't change H[u]
    // dp[u][x] = sum(dp[v][H[u]]), for x <= H[u]
    //
    // Case 2: Change H[u]
    // dp[u][x] = C[u] + sum(dp[v][x])

    static vector<int> vis(N);
    static vector<int> outdeg(N);

    for (auto u : component) outdeg[A[u]] += 1;

    vector<int> tree;
    for (auto u : component) if (outdeg[u] == 0) {
      vis[u] = 1;
      tree.emplace_back(u);
    }
    for (int i = 0; i < int(tree.size()); i++) {
      int u = tree[i];
      outdeg[A[u]]--;
      if (outdeg[A[u]] == 0) {
        vis[A[u]] = 1;
        tree.emplace_back(A[u]);
      }
    }

    long long sum_cost_cycle = 0;
    map<int, long long> value_sum_cost;
    value_sum_cost[0] = 0;
    for (auto u : component) {
      if (!vis[u]) {
        sum_cost_cycle += C[u];
        value_sum_cost[H[u]] += C[u];
      }
    }

    // For x > H[u]: dp[u][x] = C[u] + sum(dp[v][x])
    //   diff[H[u] + 1] += C[u]
    //
    // For x <= H[u]: dp[u][x] = min(C[u] + sum(dp[v][x]), sum(dp[v][H[u]]))
    //   Let x be greatest, s.t. sum(dp[v][x]) + C[u] < sum(dp[v][H[u]]):
    //   Then C[u] < sum_{x < y <= H[u]}(diff[y])
    //   But for all x + 1 <= y <= H[u]; diff[y] changes to 0.
    //   We can iterate for all y, backwards
    //
    //   diff[x + 1] = sum(dp[v][H[u]]) - sum(dp[v][x]) - C[u]
    //   diff[x + 1] = sum_{x < y <= H[u]}(diff[y]) - C[u]
    //   diff[0] += C[u]

    static vector<map<int, long long>> diff(N + 1);
    diff[N].clear();
    for (auto u : tree) {
      if (!vis[A[u]]) A[u] = N;

      diff[u][H[u] + 1] += C[u];

      long long diffsum = 0;
      while (diff[u].find(H[u] + 1) != begin(diff[u])) {
        auto d = *prev(diff[u].find(H[u] + 1));
        diff[u].erase(diff[u].find(d.first));
        diffsum += d.second;

        if (C[u] < diffsum) {
          diff[u][d.first] += diffsum - C[u];
          diff[u][0] += C[u];
          diffsum = -1;
          break;
        }
      }
      if (diffsum != -1) {
        diff[u][0] += diffsum;
      }

      // Propagate to parent
      if (diff[A[u]].size() < diff[u].size()) {
        swap(diff[A[u]], diff[u]);
      }
      for (auto d : diff[u]) {
        diff[A[u]][d.first] += d.second;
      }
    }

    map<int, long long> dp;
    for (auto [h, sub] : value_sum_cost) {
      dp[h] = 0;
    }
    for (auto [h, d] : diff[N]) {
      dp[h] = 0;
    }
    long long sum = 0;
    for (auto [h, v] : dp) {
      sum += diff[N][h];
      dp[h] = sum;
    }

    long long ans = 1e18;
    for (auto [h, sub] : value_sum_cost) {
      ans = min(ans, sum_cost_cycle - sub + dp[h]);
    }

    return ans;
  };

  long long ans = 0;

  // Solve for each connected component
  vector<vector<int>> adj(N);
  for (int i = 0; i < N; i++) {
    adj[A[i]].emplace_back(i);
    adj[i].emplace_back(A[i]);
  }
  vector<int> done(N);
  for (int i = 0; i < N; i++) if (!done[i]) {
    done[i] = 1;
    vector<int> que = {i};
    for (int q = 0; q < int(que.size()); q++) {
      int u = que[q];
      for (auto v : adj[u]) if (!done[v]) {
        done[v] = 1;
        que.emplace_back(v);
      }
    }
    ans += Solve(que);
  }

  cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 10 ms 2636 KB Output is correct
6 Correct 7 ms 1740 KB Output is correct
7 Correct 7 ms 1484 KB Output is correct
8 Correct 10 ms 2668 KB Output is correct
9 Correct 7 ms 1740 KB Output is correct
10 Correct 6 ms 1532 KB Output is correct
11 Correct 4 ms 1228 KB Output is correct
12 Correct 7 ms 972 KB Output is correct
13 Correct 4 ms 972 KB Output is correct
14 Correct 7 ms 1056 KB Output is correct
15 Correct 6 ms 972 KB Output is correct
16 Correct 10 ms 2680 KB Output is correct
17 Correct 7 ms 1868 KB Output is correct
18 Correct 4 ms 1228 KB Output is correct
19 Correct 8 ms 1568 KB Output is correct
20 Correct 6 ms 1228 KB Output is correct
21 Correct 4 ms 1232 KB Output is correct
22 Correct 8 ms 2252 KB Output is correct
23 Correct 5 ms 1612 KB Output is correct
24 Correct 10 ms 1612 KB Output is correct
25 Correct 5 ms 1356 KB Output is correct
26 Correct 5 ms 972 KB Output is correct
27 Correct 8 ms 1868 KB Output is correct
28 Correct 9 ms 1740 KB Output is correct
29 Correct 10 ms 1656 KB Output is correct
30 Correct 10 ms 1684 KB Output is correct
31 Correct 8 ms 1740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 2596 KB Output is correct
2 Correct 696 ms 112416 KB Output is correct
3 Correct 506 ms 72300 KB Output is correct
4 Correct 724 ms 111684 KB Output is correct
5 Correct 504 ms 72244 KB Output is correct
6 Correct 329 ms 45964 KB Output is correct
7 Correct 309 ms 40420 KB Output is correct
8 Correct 309 ms 28860 KB Output is correct
9 Correct 266 ms 28884 KB Output is correct
10 Correct 156 ms 28844 KB Output is correct
11 Correct 314 ms 28764 KB Output is correct
12 Correct 264 ms 28972 KB Output is correct
13 Correct 580 ms 122576 KB Output is correct
14 Correct 405 ms 77860 KB Output is correct
15 Correct 164 ms 40164 KB Output is correct
16 Correct 437 ms 54180 KB Output is correct
17 Correct 221 ms 40308 KB Output is correct
18 Correct 152 ms 40152 KB Output is correct
19 Correct 608 ms 78572 KB Output is correct
20 Correct 188 ms 53596 KB Output is correct
21 Correct 548 ms 54876 KB Output is correct
22 Correct 217 ms 40760 KB Output is correct
23 Correct 153 ms 28824 KB Output is correct
24 Correct 397 ms 61400 KB Output is correct
25 Correct 354 ms 53848 KB Output is correct
26 Correct 367 ms 52804 KB Output is correct
27 Correct 493 ms 58988 KB Output is correct
28 Correct 478 ms 58924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 10 ms 2636 KB Output is correct
6 Correct 7 ms 1740 KB Output is correct
7 Correct 7 ms 1484 KB Output is correct
8 Correct 10 ms 2668 KB Output is correct
9 Correct 7 ms 1740 KB Output is correct
10 Correct 6 ms 1532 KB Output is correct
11 Correct 4 ms 1228 KB Output is correct
12 Correct 7 ms 972 KB Output is correct
13 Correct 4 ms 972 KB Output is correct
14 Correct 7 ms 1056 KB Output is correct
15 Correct 6 ms 972 KB Output is correct
16 Correct 10 ms 2680 KB Output is correct
17 Correct 7 ms 1868 KB Output is correct
18 Correct 4 ms 1228 KB Output is correct
19 Correct 8 ms 1568 KB Output is correct
20 Correct 6 ms 1228 KB Output is correct
21 Correct 4 ms 1232 KB Output is correct
22 Correct 8 ms 2252 KB Output is correct
23 Correct 5 ms 1612 KB Output is correct
24 Correct 10 ms 1612 KB Output is correct
25 Correct 5 ms 1356 KB Output is correct
26 Correct 5 ms 972 KB Output is correct
27 Correct 8 ms 1868 KB Output is correct
28 Correct 9 ms 1740 KB Output is correct
29 Correct 10 ms 1656 KB Output is correct
30 Correct 10 ms 1684 KB Output is correct
31 Correct 8 ms 1740 KB Output is correct
32 Correct 11 ms 2596 KB Output is correct
33 Correct 696 ms 112416 KB Output is correct
34 Correct 506 ms 72300 KB Output is correct
35 Correct 724 ms 111684 KB Output is correct
36 Correct 504 ms 72244 KB Output is correct
37 Correct 329 ms 45964 KB Output is correct
38 Correct 309 ms 40420 KB Output is correct
39 Correct 309 ms 28860 KB Output is correct
40 Correct 266 ms 28884 KB Output is correct
41 Correct 156 ms 28844 KB Output is correct
42 Correct 314 ms 28764 KB Output is correct
43 Correct 264 ms 28972 KB Output is correct
44 Correct 580 ms 122576 KB Output is correct
45 Correct 405 ms 77860 KB Output is correct
46 Correct 164 ms 40164 KB Output is correct
47 Correct 437 ms 54180 KB Output is correct
48 Correct 221 ms 40308 KB Output is correct
49 Correct 152 ms 40152 KB Output is correct
50 Correct 608 ms 78572 KB Output is correct
51 Correct 188 ms 53596 KB Output is correct
52 Correct 548 ms 54876 KB Output is correct
53 Correct 217 ms 40760 KB Output is correct
54 Correct 153 ms 28824 KB Output is correct
55 Correct 397 ms 61400 KB Output is correct
56 Correct 354 ms 53848 KB Output is correct
57 Correct 367 ms 52804 KB Output is correct
58 Correct 493 ms 58988 KB Output is correct
59 Correct 478 ms 58924 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 0 ms 204 KB Output is correct
64 Correct 679 ms 80256 KB Output is correct
65 Correct 467 ms 52952 KB Output is correct
66 Correct 622 ms 74284 KB Output is correct
67 Correct 475 ms 55492 KB Output is correct
68 Correct 364 ms 39264 KB Output is correct
69 Correct 274 ms 40148 KB Output is correct
70 Correct 531 ms 57920 KB Output is correct
71 Correct 256 ms 31404 KB Output is correct
72 Correct 622 ms 69636 KB Output is correct
73 Correct 237 ms 32200 KB Output is correct
74 Correct 583 ms 70060 KB Output is correct
75 Correct 368 ms 45100 KB Output is correct
76 Correct 257 ms 45036 KB Output is correct
77 Correct 501 ms 70488 KB Output is correct
78 Correct 247 ms 45512 KB Output is correct
79 Correct 590 ms 86220 KB Output is correct
80 Correct 383 ms 54864 KB Output is correct
81 Correct 266 ms 41516 KB Output is correct
82 Correct 524 ms 69828 KB Output is correct
83 Correct 211 ms 30504 KB Output is correct
84 Correct 491 ms 57040 KB Output is correct
85 Correct 468 ms 56992 KB Output is correct
86 Correct 481 ms 53816 KB Output is correct
87 Correct 493 ms 56960 KB Output is correct
88 Correct 522 ms 56876 KB Output is correct