Submission #389274

# Submission time Handle Problem Language Result Execution time Memory
389274 2021-04-14T02:06:18 Z rama_pang Worst Reporter 4 (JOI21_worst_reporter4) C++17
79 / 100
693 ms 127660 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]);
      }
    }

    vector<int> cycle;
    long long sum_cost_cycle = 0;
    map<int, int> value_sum_cost;
    for (auto u : component) {
      if (!vis[u]) {
        cycle.emplace_back(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; dp[0] = 0;
    long long sum = 0;
    for (auto d : diff[N]) {
      sum += d.second;
      dp[d.first] = sum;
    }
    const auto Get = [&](int x) {
      return prev(dp.lower_bound({x + 1}))->second;
    };

    long long ans = Get(0) + sum_cost_cycle;
    for (auto u : cycle) {
      ans = min(ans, sum_cost_cycle - value_sum_cost[H[u]] + Get(H[u]));
    }

    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 2764 KB Output is correct
6 Correct 8 ms 1996 KB Output is correct
7 Correct 8 ms 1612 KB Output is correct
8 Correct 12 ms 2672 KB Output is correct
9 Correct 8 ms 1868 KB Output is correct
10 Correct 6 ms 1612 KB Output is correct
11 Correct 4 ms 1356 KB Output is correct
12 Correct 7 ms 1100 KB Output is correct
13 Correct 4 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 2820 KB Output is correct
17 Correct 7 ms 2016 KB Output is correct
18 Correct 4 ms 1360 KB Output is correct
19 Correct 8 ms 1708 KB Output is correct
20 Correct 5 ms 1356 KB Output is correct
21 Correct 4 ms 1356 KB Output is correct
22 Correct 7 ms 2380 KB Output is correct
23 Correct 5 ms 1740 KB Output is correct
24 Correct 11 ms 1768 KB Output is correct
25 Correct 5 ms 1484 KB Output is correct
26 Correct 4 ms 1140 KB Output is correct
27 Correct 8 ms 2068 KB Output is correct
28 Correct 7 ms 1868 KB Output is correct
29 Correct 7 ms 1736 KB Output is correct
30 Correct 8 ms 1828 KB Output is correct
31 Correct 8 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2764 KB Output is correct
2 Correct 693 ms 117308 KB Output is correct
3 Correct 523 ms 77340 KB Output is correct
4 Correct 691 ms 116796 KB Output is correct
5 Correct 496 ms 77068 KB Output is correct
6 Correct 326 ms 50988 KB Output is correct
7 Correct 268 ms 45284 KB Output is correct
8 Correct 319 ms 34088 KB Output is correct
9 Correct 262 ms 34004 KB Output is correct
10 Correct 146 ms 33996 KB Output is correct
11 Correct 313 ms 33964 KB Output is correct
12 Correct 277 ms 33996 KB Output is correct
13 Correct 575 ms 127660 KB Output is correct
14 Correct 390 ms 82860 KB Output is correct
15 Correct 161 ms 45264 KB Output is correct
16 Correct 408 ms 59176 KB Output is correct
17 Correct 217 ms 45340 KB Output is correct
18 Correct 155 ms 45260 KB Output is correct
19 Correct 542 ms 82760 KB Output is correct
20 Correct 183 ms 57892 KB Output is correct
21 Correct 491 ms 60144 KB Output is correct
22 Correct 215 ms 45868 KB Output is correct
23 Correct 153 ms 34216 KB Output is correct
24 Correct 366 ms 65688 KB Output is correct
25 Correct 329 ms 58096 KB Output is correct
26 Correct 331 ms 57000 KB Output is correct
27 Correct 458 ms 63532 KB Output is correct
28 Correct 454 ms 63428 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 2764 KB Output is correct
6 Correct 8 ms 1996 KB Output is correct
7 Correct 8 ms 1612 KB Output is correct
8 Correct 12 ms 2672 KB Output is correct
9 Correct 8 ms 1868 KB Output is correct
10 Correct 6 ms 1612 KB Output is correct
11 Correct 4 ms 1356 KB Output is correct
12 Correct 7 ms 1100 KB Output is correct
13 Correct 4 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 2820 KB Output is correct
17 Correct 7 ms 2016 KB Output is correct
18 Correct 4 ms 1360 KB Output is correct
19 Correct 8 ms 1708 KB Output is correct
20 Correct 5 ms 1356 KB Output is correct
21 Correct 4 ms 1356 KB Output is correct
22 Correct 7 ms 2380 KB Output is correct
23 Correct 5 ms 1740 KB Output is correct
24 Correct 11 ms 1768 KB Output is correct
25 Correct 5 ms 1484 KB Output is correct
26 Correct 4 ms 1140 KB Output is correct
27 Correct 8 ms 2068 KB Output is correct
28 Correct 7 ms 1868 KB Output is correct
29 Correct 7 ms 1736 KB Output is correct
30 Correct 8 ms 1828 KB Output is correct
31 Correct 8 ms 1880 KB Output is correct
32 Correct 10 ms 2764 KB Output is correct
33 Correct 693 ms 117308 KB Output is correct
34 Correct 523 ms 77340 KB Output is correct
35 Correct 691 ms 116796 KB Output is correct
36 Correct 496 ms 77068 KB Output is correct
37 Correct 326 ms 50988 KB Output is correct
38 Correct 268 ms 45284 KB Output is correct
39 Correct 319 ms 34088 KB Output is correct
40 Correct 262 ms 34004 KB Output is correct
41 Correct 146 ms 33996 KB Output is correct
42 Correct 313 ms 33964 KB Output is correct
43 Correct 277 ms 33996 KB Output is correct
44 Correct 575 ms 127660 KB Output is correct
45 Correct 390 ms 82860 KB Output is correct
46 Correct 161 ms 45264 KB Output is correct
47 Correct 408 ms 59176 KB Output is correct
48 Correct 217 ms 45340 KB Output is correct
49 Correct 155 ms 45260 KB Output is correct
50 Correct 542 ms 82760 KB Output is correct
51 Correct 183 ms 57892 KB Output is correct
52 Correct 491 ms 60144 KB Output is correct
53 Correct 215 ms 45868 KB Output is correct
54 Correct 153 ms 34216 KB Output is correct
55 Correct 366 ms 65688 KB Output is correct
56 Correct 329 ms 58096 KB Output is correct
57 Correct 331 ms 57000 KB Output is correct
58 Correct 458 ms 63532 KB Output is correct
59 Correct 454 ms 63428 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 629 ms 85492 KB Output is correct
65 Correct 498 ms 58124 KB Output is correct
66 Correct 663 ms 79468 KB Output is correct
67 Correct 486 ms 60588 KB Output is correct
68 Incorrect 330 ms 44376 KB Output isn't correct
69 Halted 0 ms 0 KB -