제출 #744773

#제출 시각아이디문제언어결과실행 시간메모리
744773t6twotwo밀림 점프 (APIO21_jumps)C++17
4 / 100
1024 ms36308 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; int N; vector<int> H, hi, lo; bool subtask1; vector<vector<int>> adj, par; void init(int n, vector<int> h) { N = n, H = h; 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[H[i]].push_back(H[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[H[i]].push_back(H[stk.back()]); } stk.push_back(i); } lo.resize(N, N); hi.resize(N, N); for (int i = 0; i < N; i++) { if (adj[i].size() == 1) { hi[i] = adj[i][0]; } if (adj[i].size() == 2) { tie(lo[i], hi[i]) = minmax(adj[i][0], adj[i][1]); } } par.resize(N + 1, vector<int>(18, N)); for (int i = 0; i < N; i++) { par[i][0] = hi[i]; } for (int j = 0; j < 17; j++) { for (int i = 0; i < N; i++) { par[i][j + 1] = par[par[i][j]][j]; } } } int minimum_jumps(int A, int B, int C, int D) { if (subtask1) { return C - B; } if (1) { int ans = N; for (int i = A; i <= B; i++) { for (int j = C; j <= D; j++) { int x = H[i]; int y = H[j]; int cnt = 0; while (x < y) { cnt++; if (hi[x] <= y) { x = hi[x]; } else if (lo[x] <= y) { x = lo[x]; } else { break; } } if (x == y) { ans = min(ans, cnt); } } } return ans; } vector<int> dis(N, N); queue<int> q; for (int i = A; i <= B; i++) { dis[H[i]] = 0; q.push(H[i]); } while (!q.empty()) { int x = q.front(); q.pop(); for (int y : adj[x]) { if (dis[y] == N) { dis[y] = dis[x] + 1; q.push(y); } } } int ans = N; for (int i = C; i <= D; i++) { ans = min(ans, dis[H[i]]); } if (ans == N) { 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...