제출 #561312

#제출 시각아이디문제언어결과실행 시간메모리
561312InternetPerson10밀림 점프 (APIO21_jumps)C++17
27 / 100
1621 ms75852 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) { 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); } } 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; } int minimum_jumps(int A, int B, int C, int D) { if(subtask == 1) { return C - B; } for(int i = 19; i >= 0; i--) { if(liftLeft[B][i] == -1) continue; if(liftLeft[B][i] < A) continue; B = liftLeft[B][i]; } int ans = single_min_jumps(B, C, D); if(ans == BIG) return -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...