Submission #744825

#TimeUsernameProblemLanguageResultExecution timeMemory
744825t6twotwoRainforest Jumps (APIO21_jumps)C++17
60 / 100
4073 ms55208 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; int N; vector<int> H; bool subtask1; vector<vector<int>> adj, hi, lo; void init(int n, vector<int> h) { N = n, H = h; H.push_back(N); subtask1 = 1; for (int i = 0; i < N; i++) { H[i]--; if (i != H[i]) { subtask1 = 0; } } adj.resize(N); vector<int> stk; for (int i = N - 1; i >= 0; i--) { while (!stk.empty() && H[stk.back()] < H[i]) { stk.pop_back(); } if (!stk.empty()) { adj[i].push_back(stk.back()); } stk.push_back(i); } stk.clear(); for (int i = 0; i < N; i++) { while (!stk.empty() && H[stk.back()] < H[i]) { stk.pop_back(); } if (!stk.empty()) { adj[i].push_back(stk.back()); } stk.push_back(i); } lo.resize(N + 1, vector<int>(18, N)); hi.resize(N + 1, vector<int>(18, N)); for (int i = 0; i < N; i++) { if (adj[i].size() == 1) { hi[i][0] = adj[i][0]; } if (adj[i].size() == 2) { if (H[adj[i][0]] > H[adj[i][1]]) { swap(adj[i][0], adj[i][1]); } lo[i][0] = adj[i][0]; hi[i][0] = adj[i][1]; } } for (int j = 0; j < 17; j++) { for (int i = 0; i < N; i++) { lo[i][j + 1] = lo[lo[i][j]][j]; hi[i][j + 1] = hi[hi[i][j]][j]; } } } int jump(int x, int y) { int ans = 0; for (int i = 17; i >= 0; i--) { if (H[hi[x][i]] <= H[y]) { ans += 1 << i; x = hi[x][i]; } } for (int i = 17; i >= 0; i--) { if (H[lo[x][i]] <= H[y]) { ans += 1 << i; x = lo[x][i]; } } if (x != y) { ans = N; } return ans; } int minimum_jumps(int A, int B, int C, int D) { if (subtask1) { return C - B; } if (C == D) { int p = A; for (int i = A; i <= B; i++) { if (H[i] > H[p]) { p = i; } } int ans = N; for (int i = C; i <= D; i++) { ans = min(ans, jump(p, i)); for (int j = p + 1; j <= B; j++) { bool u = A <= hi[j][0] && hi[j][0] <= B; bool v = A <= lo[j][0] && lo[j][0] <= B; if (u && !v && H[lo[j][0]] <= H[i]) { ans = min(ans, jump(j, i)); break; } if (v && !u && H[hi[j][0]] <= H[i]) { ans = min(ans, jump(j, i)); break; } } for (int j = p - 1; j >= A; j--) { bool u = A <= hi[j][0] && hi[j][0] <= B; bool v = A <= lo[j][0] && lo[j][0] <= B; if (u && !v && H[lo[j][0]] <= H[i]) { ans = min(ans, jump(j, i)); break; } if (v && !u && H[hi[j][0]] <= H[i]) { ans = min(ans, jump(j, i)); break; } } } if (ans == N) { ans = -1; } return ans; } if (A == B && C == D) { int ans = jump(A, C); if (ans == N) { ans = -1; } return ans; } vector<int> dis(N, N); queue<int> q; for (int i = A; i <= B; i++) { dis[i] = 0; q.push(i); } while (!q.empty()) { int x = q.front(); q.pop(); if (C <= x && x <= D) { return dis[x]; } for (int y : adj[x]) { if (dis[y] == N) { dis[y] = dis[x] + 1; q.push(y); } } } return -1; }
#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...