Submission #389283

# Submission time Handle Problem Language Result Execution time Memory
389283 2021-04-14T02:13:02 Z rama_pang Worst Reporter 4 (JOI21_worst_reporter4) C++17
79 / 100
715 ms 123160 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, int> 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 320 KB Output is correct
5 Correct 11 ms 2640 KB Output is correct
6 Correct 9 ms 1868 KB Output is correct
7 Correct 6 ms 1640 KB Output is correct
8 Correct 10 ms 2668 KB Output is correct
9 Correct 7 ms 1864 KB Output is correct
10 Correct 6 ms 1612 KB Output is correct
11 Correct 4 ms 1384 KB Output is correct
12 Correct 7 ms 1092 KB Output is correct
13 Correct 5 ms 1100 KB Output is correct
14 Correct 7 ms 1100 KB Output is correct
15 Correct 6 ms 1100 KB Output is correct
16 Correct 10 ms 2764 KB Output is correct
17 Correct 7 ms 1996 KB Output is correct
18 Correct 4 ms 1356 KB Output is correct
19 Correct 8 ms 1732 KB Output is correct
20 Correct 5 ms 1356 KB Output is correct
21 Correct 6 ms 1356 KB Output is correct
22 Correct 8 ms 2380 KB Output is correct
23 Correct 5 ms 1740 KB Output is correct
24 Correct 9 ms 1772 KB Output is correct
25 Correct 5 ms 1396 KB Output is correct
26 Correct 6 ms 1100 KB Output is correct
27 Correct 8 ms 1996 KB Output is correct
28 Correct 9 ms 1740 KB Output is correct
29 Correct 8 ms 1740 KB Output is correct
30 Correct 9 ms 1868 KB Output is correct
31 Correct 8 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 2676 KB Output is correct
2 Correct 708 ms 112768 KB Output is correct
3 Correct 522 ms 72772 KB Output is correct
4 Correct 715 ms 112356 KB Output is correct
5 Correct 503 ms 72620 KB Output is correct
6 Correct 341 ms 46536 KB Output is correct
7 Correct 279 ms 40912 KB Output is correct
8 Correct 312 ms 29488 KB Output is correct
9 Correct 285 ms 29352 KB Output is correct
10 Correct 144 ms 29356 KB Output is correct
11 Correct 310 ms 29356 KB Output is correct
12 Correct 264 ms 29384 KB Output is correct
13 Correct 600 ms 123160 KB Output is correct
14 Correct 382 ms 78628 KB Output is correct
15 Correct 161 ms 40748 KB Output is correct
16 Correct 454 ms 54740 KB Output is correct
17 Correct 217 ms 40808 KB Output is correct
18 Correct 153 ms 40748 KB Output is correct
19 Correct 598 ms 79104 KB Output is correct
20 Correct 189 ms 54136 KB Output is correct
21 Correct 523 ms 55512 KB Output is correct
22 Correct 235 ms 41196 KB Output is correct
23 Correct 152 ms 29388 KB Output is correct
24 Correct 422 ms 62008 KB Output is correct
25 Correct 358 ms 54384 KB Output is correct
26 Correct 360 ms 53296 KB Output is correct
27 Correct 489 ms 59452 KB Output is correct
28 Correct 521 ms 59472 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 320 KB Output is correct
5 Correct 11 ms 2640 KB Output is correct
6 Correct 9 ms 1868 KB Output is correct
7 Correct 6 ms 1640 KB Output is correct
8 Correct 10 ms 2668 KB Output is correct
9 Correct 7 ms 1864 KB Output is correct
10 Correct 6 ms 1612 KB Output is correct
11 Correct 4 ms 1384 KB Output is correct
12 Correct 7 ms 1092 KB Output is correct
13 Correct 5 ms 1100 KB Output is correct
14 Correct 7 ms 1100 KB Output is correct
15 Correct 6 ms 1100 KB Output is correct
16 Correct 10 ms 2764 KB Output is correct
17 Correct 7 ms 1996 KB Output is correct
18 Correct 4 ms 1356 KB Output is correct
19 Correct 8 ms 1732 KB Output is correct
20 Correct 5 ms 1356 KB Output is correct
21 Correct 6 ms 1356 KB Output is correct
22 Correct 8 ms 2380 KB Output is correct
23 Correct 5 ms 1740 KB Output is correct
24 Correct 9 ms 1772 KB Output is correct
25 Correct 5 ms 1396 KB Output is correct
26 Correct 6 ms 1100 KB Output is correct
27 Correct 8 ms 1996 KB Output is correct
28 Correct 9 ms 1740 KB Output is correct
29 Correct 8 ms 1740 KB Output is correct
30 Correct 9 ms 1868 KB Output is correct
31 Correct 8 ms 1876 KB Output is correct
32 Correct 11 ms 2676 KB Output is correct
33 Correct 708 ms 112768 KB Output is correct
34 Correct 522 ms 72772 KB Output is correct
35 Correct 715 ms 112356 KB Output is correct
36 Correct 503 ms 72620 KB Output is correct
37 Correct 341 ms 46536 KB Output is correct
38 Correct 279 ms 40912 KB Output is correct
39 Correct 312 ms 29488 KB Output is correct
40 Correct 285 ms 29352 KB Output is correct
41 Correct 144 ms 29356 KB Output is correct
42 Correct 310 ms 29356 KB Output is correct
43 Correct 264 ms 29384 KB Output is correct
44 Correct 600 ms 123160 KB Output is correct
45 Correct 382 ms 78628 KB Output is correct
46 Correct 161 ms 40748 KB Output is correct
47 Correct 454 ms 54740 KB Output is correct
48 Correct 217 ms 40808 KB Output is correct
49 Correct 153 ms 40748 KB Output is correct
50 Correct 598 ms 79104 KB Output is correct
51 Correct 189 ms 54136 KB Output is correct
52 Correct 523 ms 55512 KB Output is correct
53 Correct 235 ms 41196 KB Output is correct
54 Correct 152 ms 29388 KB Output is correct
55 Correct 422 ms 62008 KB Output is correct
56 Correct 358 ms 54384 KB Output is correct
57 Correct 360 ms 53296 KB Output is correct
58 Correct 489 ms 59452 KB Output is correct
59 Correct 521 ms 59472 KB Output is correct
60 Correct 1 ms 320 KB Output is correct
61 Correct 1 ms 320 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 702 ms 80540 KB Output is correct
65 Correct 472 ms 53276 KB Output is correct
66 Correct 634 ms 74664 KB Output is correct
67 Correct 526 ms 55724 KB Output is correct
68 Incorrect 343 ms 39496 KB Output isn't correct
69 Halted 0 ms 0 KB -