# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
390017 | 2021-04-15T05:28:07 Z | georgerapeanu | Worst Reporter 4 (JOI21_worst_reporter4) | C++11 | 451 ms | 524292 KB |
#include <bits/stdc++.h> using namespace std; struct slope_magic_t{ map<int,long long> slope_change; slope_magic_t(){ slope_change = map<int,long long>(); } void merge(slope_magic_t &other){ for(auto it:other.slope_change){ slope_change[it.first] += it.second; } } void add_value(int value,int cost){ slope_change[0] += cost; slope_change[value] -= cost; slope_change[value + 1] += cost; long long relative_height = 0; map<int,long long> :: iterator it,it2; it = slope_change.lower_bound(value); long long current_diff = slope_change[value]; while(current_diff < 0){ it2 = it; if(it2 == slope_change.begin()){ break; } it2--; if(it2 == slope_change.begin() || current_diff + it2->second > 0){ it2->second += current_diff; slope_change.erase(it); break; } relative_height = -current_diff - it2->second; slope_change.erase(it2); current_diff = -relative_height; } } int size(){ return (int)slope_change.size(); } void swap(slope_magic_t &other){ slope_change.swap(other.slope_change); } }; const int NMAX = 2e5; int n; slope_magic_t dp[NMAX + 5]; vector<int> graph[NMAX + 5]; vector<int> graph2[NMAX + 5]; int father[NMAX + 5]; int c[NMAX + 5]; int h[NMAX + 5]; bool viz[NMAX + 5]; void dfs(int nod){ viz[nod] = true; for(auto it:graph[nod]){ dfs(it); if(dp[it].size() > dp[nod].size()){ dp[nod].swap(dp[it]); } dp[nod].merge(dp[it]); } dp[nod].add_value(h[nod],c[nod]); } int lvl[NMAX + 5]; int st[NMAX + 5]; int len; vector<int> cycle; bool on_cycle[NMAX + 5]; void predfs(int nod){ st[++len] = nod; lvl[nod] = len; viz[nod] = true; for(auto it:graph2[nod]){ if(viz[it] == false){ predfs(it); }else{ for(int i = lvl[it];i <= lvl[nod];i++){ on_cycle[st[i]] = true; cycle.push_back(st[i]); } } } len--; } long long solve(int nod){ cycle.clear(); predfs(nod); slope_magic_t global; for(auto it:cycle){ for(auto it2:graph[it]){ if(on_cycle[it2] == false){ dfs(it2); global.merge(dp[it2]); } } } for(auto it:cycle){ global.slope_change[0] += c[it]; global.slope_change[h[it]] -= c[it]; global.slope_change[h[it] + 1] += c[it]; } long long curr_cost = 0; long long ans = 2e18; for(auto it:global.slope_change){ curr_cost += it.second; ans = min(ans,curr_cost); } return ans; } int main(){ scanf("%d",&n); for(int i = 1;i <= n;i++){ scanf("%d %d %d",&father[i],&h[i],&c[i]); if(father[i] == i ){ continue; } graph[father[i]].push_back(i); graph2[i].push_back(father[i]); } long long ans = 0; /* for(int i = 1;i <= n;i++){ if(viz[i] == false){ long long tmp = solve(i); ans += tmp; } }*/ dfs(1); printf("%lld\n",dp[1].slope_change[0]); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 13 ms | 19040 KB | Output is correct |
2 | Correct | 14 ms | 19020 KB | Output is correct |
3 | Correct | 14 ms | 19020 KB | Output is correct |
4 | Correct | 16 ms | 19096 KB | Output is correct |
5 | Correct | 21 ms | 20644 KB | Output is correct |
6 | Correct | 20 ms | 20172 KB | Output is correct |
7 | Correct | 24 ms | 19916 KB | Output is correct |
8 | Correct | 21 ms | 20652 KB | Output is correct |
9 | Correct | 20 ms | 20104 KB | Output is correct |
10 | Correct | 19 ms | 19888 KB | Output is correct |
11 | Correct | 18 ms | 19756 KB | Output is correct |
12 | Correct | 19 ms | 20128 KB | Output is correct |
13 | Correct | 18 ms | 20172 KB | Output is correct |
14 | Correct | 19 ms | 19932 KB | Output is correct |
15 | Correct | 18 ms | 19900 KB | Output is correct |
16 | Correct | 22 ms | 20992 KB | Output is correct |
17 | Correct | 20 ms | 20396 KB | Output is correct |
18 | Correct | 21 ms | 19736 KB | Output is correct |
19 | Correct | 19 ms | 20300 KB | Output is correct |
20 | Correct | 18 ms | 20148 KB | Output is correct |
21 | Correct | 17 ms | 20064 KB | Output is correct |
22 | Correct | 19 ms | 20388 KB | Output is correct |
23 | Correct | 18 ms | 20044 KB | Output is correct |
24 | Correct | 19 ms | 20292 KB | Output is correct |
25 | Correct | 18 ms | 20172 KB | Output is correct |
26 | Correct | 19 ms | 20504 KB | Output is correct |
27 | Correct | 19 ms | 20300 KB | Output is correct |
28 | Correct | 20 ms | 20392 KB | Output is correct |
29 | Correct | 19 ms | 20384 KB | Output is correct |
30 | Correct | 19 ms | 20380 KB | Output is correct |
31 | Correct | 19 ms | 20260 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 22 ms | 20676 KB | Output is correct |
2 | Correct | 451 ms | 94148 KB | Output is correct |
3 | Correct | 370 ms | 69444 KB | Output is correct |
4 | Correct | 443 ms | 91180 KB | Output is correct |
5 | Correct | 372 ms | 69372 KB | Output is correct |
6 | Correct | 269 ms | 50320 KB | Output is correct |
7 | Correct | 249 ms | 45892 KB | Output is correct |
8 | Correct | 227 ms | 61764 KB | Output is correct |
9 | Correct | 213 ms | 61728 KB | Output is correct |
10 | Correct | 155 ms | 61808 KB | Output is correct |
11 | Correct | 219 ms | 49280 KB | Output is correct |
12 | Correct | 209 ms | 49244 KB | Output is correct |
13 | Correct | 427 ms | 119104 KB | Output is correct |
14 | Correct | 316 ms | 83316 KB | Output is correct |
15 | Correct | 160 ms | 45812 KB | Output is correct |
16 | Correct | 398 ms | 65212 KB | Output is correct |
17 | Correct | 202 ms | 58344 KB | Output is correct |
18 | Correct | 160 ms | 58236 KB | Output is correct |
19 | Correct | 378 ms | 67768 KB | Output is correct |
20 | Correct | 174 ms | 55652 KB | Output is correct |
21 | Correct | 395 ms | 66292 KB | Output is correct |
22 | Correct | 199 ms | 59156 KB | Output is correct |
23 | Correct | 329 ms | 74560 KB | Output is correct |
24 | Correct | 361 ms | 69572 KB | Output is correct |
25 | Correct | 262 ms | 71552 KB | Output is correct |
26 | Correct | 249 ms | 72992 KB | Output is correct |
27 | Correct | 279 ms | 69804 KB | Output is correct |
28 | Correct | 282 ms | 69764 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 13 ms | 19040 KB | Output is correct |
2 | Correct | 14 ms | 19020 KB | Output is correct |
3 | Correct | 14 ms | 19020 KB | Output is correct |
4 | Correct | 16 ms | 19096 KB | Output is correct |
5 | Correct | 21 ms | 20644 KB | Output is correct |
6 | Correct | 20 ms | 20172 KB | Output is correct |
7 | Correct | 24 ms | 19916 KB | Output is correct |
8 | Correct | 21 ms | 20652 KB | Output is correct |
9 | Correct | 20 ms | 20104 KB | Output is correct |
10 | Correct | 19 ms | 19888 KB | Output is correct |
11 | Correct | 18 ms | 19756 KB | Output is correct |
12 | Correct | 19 ms | 20128 KB | Output is correct |
13 | Correct | 18 ms | 20172 KB | Output is correct |
14 | Correct | 19 ms | 19932 KB | Output is correct |
15 | Correct | 18 ms | 19900 KB | Output is correct |
16 | Correct | 22 ms | 20992 KB | Output is correct |
17 | Correct | 20 ms | 20396 KB | Output is correct |
18 | Correct | 21 ms | 19736 KB | Output is correct |
19 | Correct | 19 ms | 20300 KB | Output is correct |
20 | Correct | 18 ms | 20148 KB | Output is correct |
21 | Correct | 17 ms | 20064 KB | Output is correct |
22 | Correct | 19 ms | 20388 KB | Output is correct |
23 | Correct | 18 ms | 20044 KB | Output is correct |
24 | Correct | 19 ms | 20292 KB | Output is correct |
25 | Correct | 18 ms | 20172 KB | Output is correct |
26 | Correct | 19 ms | 20504 KB | Output is correct |
27 | Correct | 19 ms | 20300 KB | Output is correct |
28 | Correct | 20 ms | 20392 KB | Output is correct |
29 | Correct | 19 ms | 20384 KB | Output is correct |
30 | Correct | 19 ms | 20380 KB | Output is correct |
31 | Correct | 19 ms | 20260 KB | Output is correct |
32 | Correct | 22 ms | 20676 KB | Output is correct |
33 | Correct | 451 ms | 94148 KB | Output is correct |
34 | Correct | 370 ms | 69444 KB | Output is correct |
35 | Correct | 443 ms | 91180 KB | Output is correct |
36 | Correct | 372 ms | 69372 KB | Output is correct |
37 | Correct | 269 ms | 50320 KB | Output is correct |
38 | Correct | 249 ms | 45892 KB | Output is correct |
39 | Correct | 227 ms | 61764 KB | Output is correct |
40 | Correct | 213 ms | 61728 KB | Output is correct |
41 | Correct | 155 ms | 61808 KB | Output is correct |
42 | Correct | 219 ms | 49280 KB | Output is correct |
43 | Correct | 209 ms | 49244 KB | Output is correct |
44 | Correct | 427 ms | 119104 KB | Output is correct |
45 | Correct | 316 ms | 83316 KB | Output is correct |
46 | Correct | 160 ms | 45812 KB | Output is correct |
47 | Correct | 398 ms | 65212 KB | Output is correct |
48 | Correct | 202 ms | 58344 KB | Output is correct |
49 | Correct | 160 ms | 58236 KB | Output is correct |
50 | Correct | 378 ms | 67768 KB | Output is correct |
51 | Correct | 174 ms | 55652 KB | Output is correct |
52 | Correct | 395 ms | 66292 KB | Output is correct |
53 | Correct | 199 ms | 59156 KB | Output is correct |
54 | Correct | 329 ms | 74560 KB | Output is correct |
55 | Correct | 361 ms | 69572 KB | Output is correct |
56 | Correct | 262 ms | 71552 KB | Output is correct |
57 | Correct | 249 ms | 72992 KB | Output is correct |
58 | Correct | 279 ms | 69804 KB | Output is correct |
59 | Correct | 282 ms | 69764 KB | Output is correct |
60 | Runtime error | 338 ms | 524292 KB | Execution killed with signal 9 |
61 | Halted | 0 ms | 0 KB | - |