제출 #554062

#제출 시각아이디문제언어결과실행 시간메모리
554062Zhora_004밀림 점프 (APIO21_jumps)C++17
4 / 100
947 ms55840 KiB
#include "jumps.h" #include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <string> #include <sstream> #include <iomanip> #include <map> #include <unordered_map> #include <stack> #include <cstdio> #include <climits> #include <tuple> #include <ctime> #include <cstring> #include <numeric> #include <functional> #include <chrono> #include <cassert> #include <bitset> #include <fstream> //#include <bit> //#include <ranges> //#include <numbers> #define sz(a) ((int)((a).size())) // printf("%.10f\n", ans); /*ofstream fout("timeline.out"); ifstream fin("timeline.in");*/ using ll = long long; using namespace std; const int inf = 1e9; bool task1; int n, a, b, c, d, ans; vector<int> h, dp, nge, pge, ind; vector<vector<int>> go1, go2; bool task(int id) { if (id == 1) { for (int i = 0; i < n; i++) if (h[i] != i + 1) return 0; return 1; } return 0; } void NGE() { stack<int> s; s.push(0); for (int i = 1; i < n; i++) { if (s.empty()) { s.push(i); continue; } while (!s.empty() && h[s.top()] < h[i]) { nge[s.top()] = i; s.pop(); } s.push(i); } while (!s.empty()) { nge[s.top()] = -1; s.pop(); } } void PGE() { stack<int> s; s.push(0); pge[0] = -1; for (int i = 1; i < n; i++) { while (!s.empty() && h[s.top()] < h[i]) s.pop(); if (s.empty()) pge[i] = -1; else pge[i] = s.top(); s.push(i); } } int dfs(int u) { if (dp[u] != inf + 1) return dp[u]; if (c <= u && u <= d) return dp[u] = 0; int r = nge[u]; int ans = inf; if (r != -1) ans = min(ans, dfs(r) + 1); int l = pge[u]; if (l != -1) ans = min(ans, dfs(l) + 1); return dp[u] = ans; } void init(int N, std::vector<int> H) { n = N; h = H; nge = pge = vector<int>(n); ind = vector<int>(n + 1); go1 = go2 = vector<vector<int>>(n, vector<int>(20)); NGE(); PGE(); if (task(1)) task1 = 1; for (int i = 0; i < n; i++) ind[h[i]] = i; for (int i = n; i >= 1; i--) { int id = ind[i]; int l = pge[id], r = nge[id]; if (l == -1 || r == -1) go1[id][0] = n; else { if (h[l] > h[r]) go1[id][0] = l; else go1[id][0] = r; } for (int j = 1; j < 20; j++) { int u = go1[id][j - 1]; if (u == n) go1[id][j] = n; else go1[id][j] = go1[u][j - 1]; } if (l == -1 && r == -1) go2[id][0] = n; else if (l == -1) go2[id][0] = r; else if (r == -1) go2[id][0] = l; else { if (h[l] < h[r]) go2[id][0] = l; else go2[id][0] = r; } for (int j = 1; j < 20; j++) { int u = go2[id][j - 1]; if (u == n) go2[id][j] = n; else go2[id][j] = go2[u][j - 1]; } } } int minimum_jumps(int A, int B, int C, int D) { a = A, b = B, c = C, d = D; if (task1) return c - b; if (a == b && c == d) { if (h[a] > h[c]) return -1; int ans = 0, id = a; for (int i = 19; i >= 0; i--) { int u = go1[id][i]; if (u < n && h[u] <= h[c]) { ans += (1 << i); id = u; } } for (int i = 19; i >= 0; i--) { int u = go2[id][i]; if (u < n && h[u] > h[c]) return -1; if (u < n && h[u] <= h[c]) { ans += (1 << i); id = u; } } return ans; } else { dp = vector<int>(n, inf + 1); ans = inf; for (int i = a; i <= b; i++) ans = min(ans, dfs(i)); if (ans == inf) 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...