답안 #397120

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
397120 2021-05-01T13:30:43 Z fedoseevtimofey Worst Reporter 4 (JOI21_worst_reporter4) C++14
79 / 100
426 ms 112936 KB
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
#include <chrono>
 
using namespace std;
 
typedef long long ll;
 
ll smart(vector <int> a, vector <int> h, vector <int> c) {
  int n = a.size();
  vector <vector <int>> g(n);
  for (int i = 0; i < n; ++i) {
    --a[i];
    if (a[i] != i) {
      g[a[i]].push_back(i);
    }
  }
  vector <map <int, ll>> dp(n);
  auto dfs = [&] (int u, int p, auto&& dfs) -> void {
    for (auto v : g[u]) {
      if (v != p) {
        dfs(v, u, dfs);
        if (dp[v].size() > dp[u].size()) swap(dp[u], dp[v]);
        for (auto p : dp[v]) dp[u][p.first] += p.second;
      }
    }
    dp[u][1] += c[u];
    dp[u][h[u]] -= c[u];
    dp[u][h[u] + 1] += c[u];

    /*cout << "start " << u + 1 << endl;
    for (auto p : dp[u]) {
      cout << p.first << ' ' << p.second << endl;
    }
    cout << "finish" << endl;*/
    auto it = dp[u].find(h[u]);
    while (it->second < 0) {
      auto jt = prev(it);
      jt->second += it->second;
      dp[u].erase(it);
      it = jt;
    }
    /*cout << "start " << u + 1 << endl;
    for (auto p : dp[u]) {
      cout << p.first << ' ' << p.second << endl;
    }
    cout << "finish" << endl;*/
  };
  dfs(0, -1, dfs);
  if (dp[0].empty() || dp[0].begin()->first > 1) {
    return 0LL;
  } else {
    return dp[0].begin()->second;
  }
}

ll stupid(vector <int> a, vector <int> h, vector <int> c) {
  int n = a.size();
  vector <vector <int>> g(n);
  for (int i = 0; i < n; ++i) {
    --a[i];
    if (a[i] != i) {
      g[a[i]].push_back(i);
    }
  }
vector <map <int, ll>> dp(n);
  auto dfs = [&] (int u, int p, auto&& dfs) -> void {
    for (auto v : g[u]) {
      if (v != p) {
        dfs(v, u, dfs);
        if (dp[v].size() > dp[u].size()) swap(dp[u], dp[v]);
        for (auto p : dp[v]) dp[u][p.first] += p.second;
      }
    }
    dp[u][1] += c[u];
    dp[u][h[u]] -= c[u];
    dp[u][h[u] + 1] += c[u];
    vector <pair <int, ll>> cur = vector <pair <int, ll>> (dp[u].begin(), dp[u].end());
    for (int i = 1; i < (int)cur.size(); ++i) cur[i].second += cur[i - 1].second;
    for (int i = (int)cur.size() - 2; i >= 0; --i) cur[i].second = min(cur[i].second, cur[i + 1].second);
    dp[u].clear();
    for (int i = 0; i < (int)cur.size(); ++i) {
      if (i == 0 || cur[i].second != cur[i - 1].second) dp[u][cur[i].first] += cur[i].second - (i == 0 ? 0 : cur[i - 1].second); 
    }
  };
  dfs(0, -1, dfs);
  ll ans = 2e18;
  ll sum = 0;
  for (auto p : dp[0]) {
    sum += p.second;
    ans = min(ans, sum);
  }
  return ans;
}

void stress() {
  mt19937 rnd(123);
  int tt = 1000;
  while (tt--) {
    int n = rnd() % 5 + 1;
    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
      a[i] = rnd() % (i + 1) + 1;
    }
    vector <int> h(n), c(n);
    for (int i = 0; i < n; ++i) {
      h[i] = rnd() % 5 + 1, c[i] = rnd() % 5 + 1;
    }
    if (smart(a, h, c) != stupid(a, h, c)) {
      cout << n << '\n';
      for (int i = 0; i < n; ++i) {
        cout << a[i] << ' ' << h[i] << ' ' << c[i] << endl;
      }
      exit(0);
    }
    if (tt % 10 == 0) {
      cout << tt << endl;
    }
  }
  exit(0);
}

void run() {
  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];
  }
  cout << smart(a, h, c) << endl;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  //stress();
  run();
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 7 ms 1884 KB Output is correct
6 Correct 6 ms 1612 KB Output is correct
7 Correct 7 ms 1356 KB Output is correct
8 Correct 9 ms 2124 KB Output is correct
9 Correct 6 ms 1636 KB Output is correct
10 Correct 6 ms 1356 KB Output is correct
11 Correct 4 ms 1228 KB Output is correct
12 Correct 5 ms 1740 KB Output is correct
13 Correct 4 ms 1732 KB Output is correct
14 Correct 5 ms 1356 KB Output is correct
15 Correct 5 ms 1356 KB Output is correct
16 Correct 8 ms 2380 KB Output is correct
17 Correct 7 ms 1868 KB Output is correct
18 Correct 5 ms 1228 KB Output is correct
19 Correct 8 ms 1768 KB Output is correct
20 Correct 5 ms 1612 KB Output is correct
21 Correct 4 ms 1612 KB Output is correct
22 Correct 5 ms 1868 KB Output is correct
23 Correct 5 ms 1484 KB Output is correct
24 Correct 8 ms 1840 KB Output is correct
25 Correct 4 ms 1616 KB Output is correct
26 Correct 5 ms 1996 KB Output is correct
27 Correct 6 ms 1740 KB Output is correct
28 Correct 5 ms 1868 KB Output is correct
29 Correct 5 ms 1868 KB Output is correct
30 Correct 7 ms 1868 KB Output is correct
31 Correct 6 ms 1784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1996 KB Output is correct
2 Correct 426 ms 83252 KB Output is correct
3 Correct 349 ms 58484 KB Output is correct
4 Correct 395 ms 80260 KB Output is correct
5 Correct 357 ms 58524 KB Output is correct
6 Correct 215 ms 39480 KB Output is correct
7 Correct 198 ms 34996 KB Output is correct
8 Correct 192 ms 53860 KB Output is correct
9 Correct 176 ms 58652 KB Output is correct
10 Correct 124 ms 58592 KB Output is correct
11 Correct 221 ms 44740 KB Output is correct
12 Correct 184 ms 44640 KB Output is correct
13 Correct 387 ms 112936 KB Output is correct
14 Correct 281 ms 77380 KB Output is correct
15 Correct 129 ms 39720 KB Output is correct
16 Correct 303 ms 60804 KB Output is correct
17 Correct 168 ms 53940 KB Output is correct
18 Correct 132 ms 53796 KB Output is correct
19 Correct 273 ms 61664 KB Output is correct
20 Correct 144 ms 49344 KB Output is correct
21 Correct 345 ms 61532 KB Output is correct
22 Correct 155 ms 54448 KB Output is correct
23 Correct 288 ms 71400 KB Output is correct
24 Correct 251 ms 62428 KB Output is correct
25 Correct 256 ms 65120 KB Output is correct
26 Correct 214 ms 67064 KB Output is correct
27 Correct 277 ms 62604 KB Output is correct
28 Correct 239 ms 62660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 7 ms 1884 KB Output is correct
6 Correct 6 ms 1612 KB Output is correct
7 Correct 7 ms 1356 KB Output is correct
8 Correct 9 ms 2124 KB Output is correct
9 Correct 6 ms 1636 KB Output is correct
10 Correct 6 ms 1356 KB Output is correct
11 Correct 4 ms 1228 KB Output is correct
12 Correct 5 ms 1740 KB Output is correct
13 Correct 4 ms 1732 KB Output is correct
14 Correct 5 ms 1356 KB Output is correct
15 Correct 5 ms 1356 KB Output is correct
16 Correct 8 ms 2380 KB Output is correct
17 Correct 7 ms 1868 KB Output is correct
18 Correct 5 ms 1228 KB Output is correct
19 Correct 8 ms 1768 KB Output is correct
20 Correct 5 ms 1612 KB Output is correct
21 Correct 4 ms 1612 KB Output is correct
22 Correct 5 ms 1868 KB Output is correct
23 Correct 5 ms 1484 KB Output is correct
24 Correct 8 ms 1840 KB Output is correct
25 Correct 4 ms 1616 KB Output is correct
26 Correct 5 ms 1996 KB Output is correct
27 Correct 6 ms 1740 KB Output is correct
28 Correct 5 ms 1868 KB Output is correct
29 Correct 5 ms 1868 KB Output is correct
30 Correct 7 ms 1868 KB Output is correct
31 Correct 6 ms 1784 KB Output is correct
32 Correct 7 ms 1996 KB Output is correct
33 Correct 426 ms 83252 KB Output is correct
34 Correct 349 ms 58484 KB Output is correct
35 Correct 395 ms 80260 KB Output is correct
36 Correct 357 ms 58524 KB Output is correct
37 Correct 215 ms 39480 KB Output is correct
38 Correct 198 ms 34996 KB Output is correct
39 Correct 192 ms 53860 KB Output is correct
40 Correct 176 ms 58652 KB Output is correct
41 Correct 124 ms 58592 KB Output is correct
42 Correct 221 ms 44740 KB Output is correct
43 Correct 184 ms 44640 KB Output is correct
44 Correct 387 ms 112936 KB Output is correct
45 Correct 281 ms 77380 KB Output is correct
46 Correct 129 ms 39720 KB Output is correct
47 Correct 303 ms 60804 KB Output is correct
48 Correct 168 ms 53940 KB Output is correct
49 Correct 132 ms 53796 KB Output is correct
50 Correct 273 ms 61664 KB Output is correct
51 Correct 144 ms 49344 KB Output is correct
52 Correct 345 ms 61532 KB Output is correct
53 Correct 155 ms 54448 KB Output is correct
54 Correct 288 ms 71400 KB Output is correct
55 Correct 251 ms 62428 KB Output is correct
56 Correct 256 ms 65120 KB Output is correct
57 Correct 214 ms 67064 KB Output is correct
58 Correct 277 ms 62604 KB Output is correct
59 Correct 239 ms 62660 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Incorrect 1 ms 204 KB Output isn't correct
62 Halted 0 ms 0 KB -