Submission #561351

#TimeUsernameProblemLanguageResultExecution timeMemory
561351InternetPerson10Rainforest Jumps (APIO21_jumps)C++17
81 / 100
4061 ms474304 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; int subtask = 1; int n; vector<int> nums, l, r; vector<vector<int>> liftBig, liftRight, liftLeft; bool invalid(int k) { if(k == -1 || k == n) return true; return false; } int bigger(int a, int b) { if(invalid(a) && invalid(b)) return -1; if(invalid(a)) return b; if(invalid(b)) return a; if(nums[a] > nums[b]) return a; return b; } void init(int N, std::vector<int> H) { bool subtask1 = true; for(int i = 0; i < N; i++) { if(H[i] != i+1) subtask1 = false; H[i]--; } if(subtask1) subtask = 1; else if(N <= 200) subtask = 2; else subtask = 3; n = N; nums = H; l.resize(n, -1); r.resize(n, n); liftBig.resize(n, vector<int>(20)); liftRight.resize(n, vector<int>(20)); liftLeft.resize(n, vector<int>(20)); vector<int> v; v.push_back(0); for(int i = 1; i < n; i++) { while(v.size()) { if(nums[v.back()] < nums[i]) { r[v.back()] = i; v.pop_back(); } else break; } v.push_back(i); } v.clear(); v.push_back(n-1); for(int i = n-2; i >= 0; i--) { while(v.size()) { if(nums[v.back()] < nums[i]) { l[v.back()] = i; v.pop_back(); } else break; } v.push_back(i); } for(int j = 0; j < N; j++) { liftBig[j][0] = bigger(l[j], r[j]); liftLeft[j][0] = l[j]; if(r[j] == n) liftRight[j][0] = -1; else liftRight[j][0] = r[j]; } for(int i = 1; i < 20; i++) { for(int j = 0; j < N; j++) { if(liftBig[j][i-1] == -1) liftBig[j][i] = -1; else liftBig[j][i] = liftBig[liftBig[j][i-1]][i-1]; if(liftRight[j][i-1] == -1) liftRight[j][i] = -1; else liftRight[j][i] = liftRight[liftRight[j][i-1]][i-1]; if(liftLeft[j][i-1] == -1) liftLeft[j][i] = -1; else liftLeft[j][i] = liftLeft[liftLeft[j][i-1]][i-1]; } } } int BIG = 1e9 + 7; int reachable(int x, int C, int D) { // With a series of right jumps if(C <= x && x <= D) return 0; if(x > D) return -1; int ans2 = BIG, tot = 0; for(int i = 19; i >= 0; i--) { if(liftRight[x][i] == -1) continue; if(liftRight[x][i] > D) continue; if(liftRight[x][i] >= C) ans2 = min(ans2, tot + (1 << i)); else { x = liftRight[x][i]; tot += (1 << i); } } if(ans2 == BIG) return -1; return ans2; } int single_min_jumps(int x, int C, int D) { int r = reachable(x, C, D); if(r == -1) return BIG; int ans = r; int tot = 0; if(subtask == 2) { while(liftBig[x][0] != -1) { x = liftBig[x][0]; tot++; r = reachable(x, C, D); if(r != -1) ans = min(ans, r + tot); } return ans; } for(int i = 19; i >= 0; i--) { if(liftBig[x][i] == -1) continue; int r = reachable(liftBig[x][i], C, D); if(r == -1) continue; ans = min(ans, tot + (1 << i) + r); if(r != 0) { x = liftBig[x][i]; tot += (1 << i); } } return ans; } map<pair<int, pair<int, int>>, int> dp; int single_min_jumps_brute(int x, int C, int D) { if(invalid(x)) return BIG; pair<int, pair<int, int>> p = {x, {C, D}}; if(dp.count(p)) return dp[p]; if(C <= x && x <= D) return dp[p] = 0; return dp[p] = min(single_min_jumps_brute(l[x], C, D), single_min_jumps_brute(r[x], C, D)) + 1; } void debug(int A, int B, int C, int D, int ans) { int ans2 = BIG; for(int i = A; i <= B; i++) { ans2 = min(ans2, single_min_jumps_brute(i, C, D)); } if(ans2 == BIG) ans2 = -1; if(ans != ans2) { cerr << "NUMBERS:\n"; for(int i = 0; i < n; i++) cerr << i << ' ' << nums[i] << '\n'; cerr << '\n'; cerr << "CASE: " << A << ' ' << B << ' ' << C << ' ' << D << '\n'; cerr << "ANS: " << ans << '\n'; cerr << "BRUTE: " << ans2 << '\n'; assert(false); } } int minimum_jumps(int A, int B, int C, int D) { if(subtask == 1) { return C - B; } int ans = BIG; if(C != D) { for(int i = A; i <= B; i++) { ans = min(ans, single_min_jumps_brute(i, C, D)); } } else { for(int i = 19; i >= 0; i--) { if(liftLeft[B][i] == -1) continue; if(liftLeft[B][i] < A) continue; if(reachable(liftLeft[B][i], C, D) == -1) continue; ans = single_min_jumps(B, C, D); B = liftLeft[B][i]; } ans = single_min_jumps(B, C, D); } if(ans == BIG) ans = -1; return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...