Submission #417399

# Submission time Handle Problem Language Result Execution time Memory
417399 2021-06-03T16:18:25 Z rama_pang Job Scheduling (IOI19_job) C++17
100 / 100
399 ms 74208 KB
#include "job.h"

#include <bits/stdc++.h>
using namespace std;

using lint = long long;

lint scheduling_cost(vector<int> P, vector<int> U, vector<int> D) {
  // Solution:
  // Assume there is no p[]. Let's ignore u[i] * d[i].
  //
  // Let's observe i and j. If i < j is optimal:
  // u[j] * d[i] < u[i] * d[j]
  // u[j] / d[j] < u[i] / d[i]
  // So, we sort by u[x] / d[x] in decreasing opt.
  // With this, we can solve for a star graph.
  //
  // Now, assume it's a tree, and we only have 2 children.
  // If the optimal ordering of the children is sorted
  // decreasingly by U[i] / D[i], then we can merge the
  // sorted list together.
  //
  // Assume we have a list = {U[i] / D[i], U[j] / D[j]} and {U[k] / D[k]}.
  // Also, U[i] / D[i] < U[j] / D[j].
  //
  // Since j comes after i, let's ignore U[j] * D[i].
  //
  // Cost if {i, j, k}:
  // U[k] * D[i] + U[k] * D[j]
  //
  // Cost if {k, i, j}:
  // U[i] * D[k] + U[j] * D[k]
  //
  // Cost if {i, k, j}:
  // U[k] * D[i] + U[j] * D[k]
  //
  // If {i, k, j} is optimal:
  // U[k] * D[i] + U[j] * D[k] < U[k] * D[i] + U[k] * D[j]
  // -> U[j] * D[k] < U[k] * D[j] -> U[j] / D[j] < U[k] / D[k]
  // U[k] * D[i] + U[j] * D[k] < U[i] * D[k] + U[j] * D[k]
  // -> U[k] * D[i] < U[i] * D[k] -> U[k] / D[k] < U[i] / D[i]
  // But U[i] / D[i] < U[j] / D[j], so contradiction.
  //
  // So only {i, j, k} or {k, i, j} can be optimal.
  // Thus, we can merge {i, j} together, so we always have a sorted
  // decreasingly list.
  //
  // With small to large, we have O(N log^2 N).

  int N = P.size();
  vector<vector<int>> adj(N);
  for (int i = 1; i < N; i++) {
    adj[P[i]].emplace_back(i);
  }

  struct Item {
    int u, d;
    Item(int u, int d) : u(u), d(d) {}
    const bool operator<(const Item &o) const {
      return 1ll * u * o.d > 1ll * o.u * d;
    }
  };

  lint ans = 0;
  for (int i = 0; i < N; i++) {
    ans += 1ll * U[i] * D[i];
  }

  vector<multiset<Item>> dp(N);
  const auto Dfs = [&](const auto &self, int u) -> void {
    for (auto v : adj[u]) {
      self(self, v);
      if (dp[u].size() < dp[v].size()) {
        swap(dp[u], dp[v]);
      }
      for (auto i : dp[v]) {
        dp[u].emplace(i);
      }
    }
    Item cur(U[u], D[u]);
    while (!dp[u].empty() && *begin(dp[u]) < cur) {
      auto it = *begin(dp[u]);
      dp[u].erase(dp[u].find(it));
      ans += 1ll * it.u * cur.d;
      cur.u += it.u;
      cur.d += it.d;
    }
    dp[u].emplace(cur);
  };

  int t = 0;
  Dfs(Dfs, 0);
  for (auto i : dp[0]) {
    ans += 1ll * t * i.u;
    t += i.d;
  }

  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 30 ms 12780 KB Output is correct
6 Correct 63 ms 24796 KB Output is correct
7 Correct 87 ms 36428 KB Output is correct
8 Correct 122 ms 48196 KB Output is correct
9 Correct 137 ms 48248 KB Output is correct
10 Correct 117 ms 48196 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 120 ms 48296 KB Output is correct
13 Correct 113 ms 48452 KB Output is correct
14 Correct 130 ms 48324 KB Output is correct
15 Correct 110 ms 48452 KB Output is correct
16 Correct 140 ms 48544 KB Output is correct
17 Correct 123 ms 48408 KB Output is correct
18 Correct 117 ms 48468 KB Output is correct
19 Correct 143 ms 48388 KB Output is correct
20 Correct 161 ms 54872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 288 ms 38800 KB Output is correct
5 Correct 279 ms 38548 KB Output is correct
6 Correct 297 ms 38584 KB Output is correct
7 Correct 264 ms 38588 KB Output is correct
8 Correct 277 ms 38628 KB Output is correct
9 Correct 283 ms 38632 KB Output is correct
10 Correct 276 ms 38584 KB Output is correct
11 Correct 283 ms 38584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 9 ms 2124 KB Output is correct
6 Correct 259 ms 39940 KB Output is correct
7 Correct 253 ms 39608 KB Output is correct
8 Correct 283 ms 39704 KB Output is correct
9 Correct 259 ms 39608 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 7 ms 1968 KB Output is correct
13 Correct 8 ms 2260 KB Output is correct
14 Correct 266 ms 39684 KB Output is correct
15 Correct 270 ms 39664 KB Output is correct
16 Correct 256 ms 39604 KB Output is correct
17 Correct 259 ms 39648 KB Output is correct
18 Correct 258 ms 39708 KB Output is correct
19 Correct 267 ms 39572 KB Output is correct
20 Correct 271 ms 39580 KB Output is correct
21 Correct 269 ms 39608 KB Output is correct
22 Correct 295 ms 39608 KB Output is correct
23 Correct 267 ms 39424 KB Output is correct
24 Correct 263 ms 39384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 116 ms 36272 KB Output is correct
3 Correct 112 ms 39452 KB Output is correct
4 Correct 113 ms 39464 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 224 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 288 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 30 ms 12780 KB Output is correct
6 Correct 63 ms 24796 KB Output is correct
7 Correct 87 ms 36428 KB Output is correct
8 Correct 122 ms 48196 KB Output is correct
9 Correct 137 ms 48248 KB Output is correct
10 Correct 117 ms 48196 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 120 ms 48296 KB Output is correct
13 Correct 113 ms 48452 KB Output is correct
14 Correct 130 ms 48324 KB Output is correct
15 Correct 110 ms 48452 KB Output is correct
16 Correct 140 ms 48544 KB Output is correct
17 Correct 123 ms 48408 KB Output is correct
18 Correct 117 ms 48468 KB Output is correct
19 Correct 143 ms 48388 KB Output is correct
20 Correct 161 ms 54872 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 288 ms 38800 KB Output is correct
25 Correct 279 ms 38548 KB Output is correct
26 Correct 297 ms 38584 KB Output is correct
27 Correct 264 ms 38588 KB Output is correct
28 Correct 277 ms 38628 KB Output is correct
29 Correct 283 ms 38632 KB Output is correct
30 Correct 276 ms 38584 KB Output is correct
31 Correct 283 ms 38584 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 9 ms 2124 KB Output is correct
37 Correct 259 ms 39940 KB Output is correct
38 Correct 253 ms 39608 KB Output is correct
39 Correct 283 ms 39704 KB Output is correct
40 Correct 259 ms 39608 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 1 ms 588 KB Output is correct
43 Correct 7 ms 1968 KB Output is correct
44 Correct 8 ms 2260 KB Output is correct
45 Correct 266 ms 39684 KB Output is correct
46 Correct 270 ms 39664 KB Output is correct
47 Correct 256 ms 39604 KB Output is correct
48 Correct 259 ms 39648 KB Output is correct
49 Correct 258 ms 39708 KB Output is correct
50 Correct 267 ms 39572 KB Output is correct
51 Correct 271 ms 39580 KB Output is correct
52 Correct 269 ms 39608 KB Output is correct
53 Correct 295 ms 39608 KB Output is correct
54 Correct 267 ms 39424 KB Output is correct
55 Correct 263 ms 39384 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 116 ms 36272 KB Output is correct
58 Correct 112 ms 39452 KB Output is correct
59 Correct 113 ms 39464 KB Output is correct
60 Correct 1 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 1 ms 204 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 1 ms 204 KB Output is correct
67 Correct 1 ms 204 KB Output is correct
68 Correct 1 ms 224 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 288 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 204 KB Output is correct
75 Correct 1 ms 204 KB Output is correct
76 Correct 1 ms 204 KB Output is correct
77 Correct 1 ms 204 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 1 ms 204 KB Output is correct
81 Correct 1 ms 204 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 0 ms 204 KB Output is correct
86 Correct 1 ms 204 KB Output is correct
87 Correct 1 ms 332 KB Output is correct
88 Correct 5 ms 1400 KB Output is correct
89 Correct 8 ms 2164 KB Output is correct
90 Correct 10 ms 2632 KB Output is correct
91 Correct 56 ms 12892 KB Output is correct
92 Correct 136 ms 26180 KB Output is correct
93 Correct 290 ms 52764 KB Output is correct
94 Correct 322 ms 53532 KB Output is correct
95 Correct 302 ms 53424 KB Output is correct
96 Correct 296 ms 53152 KB Output is correct
97 Correct 295 ms 53412 KB Output is correct
98 Correct 291 ms 57264 KB Output is correct
99 Correct 336 ms 60904 KB Output is correct
100 Correct 293 ms 52936 KB Output is correct
101 Correct 228 ms 45352 KB Output is correct
102 Correct 399 ms 74208 KB Output is correct
103 Correct 256 ms 44744 KB Output is correct
104 Correct 218 ms 39516 KB Output is correct
105 Correct 167 ms 36512 KB Output is correct
106 Correct 290 ms 50376 KB Output is correct