Submission #413909

#TimeUsernameProblemLanguageResultExecution timeMemory
413909flashmtRainforest Jumps (APIO21_jumps)C++17
35 / 100
1669 ms56036 KiB
#include <bits/stdc++.h> using namespace std; const int N = 200200; const int LG = 18; int n, l[N][LG], r[N][LG], high[N][LG]; vector<int> h; void initEdges() { vector<pair<int, int>> sortedH; for (int i = 0; i < n; i++) sortedH.push_back({h[i], i}); sort(sortedH.begin(), sortedH.end(), greater<pair<int, int>>()); set<int> s; s.insert(-1); s.insert(n); memset(l, -1, sizeof l); memset(r, -1, sizeof r); memset(high, -1, sizeof high); for (auto [_, i] : sortedH) { auto it = s.lower_bound(i); r[i][0] = *it; l[i][0] = *(--it); if (l[i][0] < 0) high[i][0] = r[i][0]; else if (r[i][0] < 0) high[i][0] = l[i][0]; else if (h[l[i][0]] > h[r[i][0]]) high[i][0] = l[i][0]; else high[i][0] = r[i][0]; s.insert(i); } } void constructTable() { for (int j = 0; j + 1 < LG; j++) for (int i = 0; i < n; i++) { if (l[i][j] >= 0 && l[i][j] < n) l[i][j + 1] = l[l[i][j]][j]; if (r[i][j] >= 0 && r[i][j] < n) r[i][j + 1] = r[r[i][j]][j]; if (high[i][j] >= 0 && high[i][j] < n) high[i][j + 1] = high[high[i][j]][j]; } } void init(int N, vector<int> H) { n = N; h = H; h[n] = 0; initEdges(); constructTable(); } int jump(int &s, int table[N][LG], function<int(int)> cond) { int res = 0; for (int i = LG - 1; i >= 0; i--) { int x = table[s][i]; if (x >= 0 && x < n && cond(x)) { s = x; res |= 1 << i; } } return res; } int minimum_jumps(int A, int B, int C, int D) { int s = B; // find where to start jump(s, l, [&](int x) { return x >= A && r[x][0] >= 0 && r[x][0] <= D; }); // use high edges as most as possible int ans = jump(s, high, [&](int x) { return r[x][0] >= 0 && r[x][0] < C; }); // check if we need to go 1 more high step if (r[s][0] >= 0 && r[s][0] < C) { int x = high[s][0]; if (x >= 0 && r[x][0] >= 0 && r[x][0] <= D) { s = x; ans++; } } // just go right ans += jump(s, r, [&](int x) { return x < C; }); return r[s][0] >= C && r[s][0] <= D ? ans + 1 : -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...