Submission #415875

#TimeUsernameProblemLanguageResultExecution timeMemory
415875valerikkRainforest Jumps (APIO21_jumps)C++17
33 / 100
4097 ms5812 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; const int INF = 1e9; int n, h[N]; int l[N], r[N], m[N]; void init(int nn, vector<int> hh) { for (int i = 0; i < nn; i++) h[i + 1] = hh[i] - 1; h[0] = nn; h[nn + 1] = nn + 1; n = nn + 2; vector<int> st; for (int i = 0; i < n; ++i) { while (!st.empty() && h[st.back()] < h[i]) { r[st.back()] = i; st.pop_back(); } st.push_back(i); } r[n - 1] = n - 1; st.clear(); for (int i = n - 1; i >= 0; --i) { while (!st.empty() && h[st.back()] < h[i]) { l[st.back()] = i; st.pop_back(); } st.push_back(i); } l[0] = 0; st.clear(); for (int i = 0; i < n; ++i) m[i] = h[l[i]] > h[r[i]] ? l[i] : r[i]; } int minimum_jumps(int aa, int bb, int cc, int dd) { int a = aa + 1, b = bb + 1, c = cc + 1, d = dd + 1; int mx = 0; for (int i = c; i <= d; ++i) mx = max(mx, h[i]); int mx1 = -INF; for (int i = b + 1; i < c; ++i) mx1 = max(mx1, h[i]); if (mx1 > mx || h[b] > mx) return -1; for (int i = a; i <= b; ++i) { if (h[i] > mx) a = max(a, i + 1); } int st = b; for (int i = a; i <= b; ++i) { if (h[i] < mx && h[i] > h[st]) st = i; } int res = 1; while (h[st] < mx1) { ++res; //cout << st - 1 << " " << h[st] + 1 << endl; if (h[m[st]] < mx) st = m[st]; else st = r[st]; } //cout << st - 1 << " " << h[st] + 1 << endl; return res; } int get_ans(int a, int b, int c, int d) { ++a, ++b, ++c, ++d; vector<int> dist(n, INF); queue<int> q; for (int i = a; i <= b; ++i) { dist[i] = 0; q.push(i); } while (!q.empty()) { int v = q.front(); q.pop(); if (v >= c && v <= d) return dist[v]; for (auto u : {l[v], r[v]}) { if (dist[v] +1 < dist[u]) { dist[u] = dist[v] + 1; q.push(u); } } } return -1; } #ifdef LOCAL int main() { freopen("input.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); if (0) { int nn, qq; cin >> nn >> qq; vector<int> hh(nn); for (auto &x : hh) cin >> x; init(nn, hh); while (qq--) { int aa, bb, cc, dd; cin >> aa >> bb >> cc >> dd; cout << minimum_jumps(aa, bb, cc, dd) << endl; } } else { srand(0); int nn = 15; vector<int> hh(nn); for (int i = 0; i < nn; ++i) hh[i] = i + 1; random_shuffle(hh.begin(), hh.end()); init(nn, hh); int qq = 1000000; while (qq--) { vector<int> v; for (int i = 0; i < 4; ++i) v.push_back(rand() % nn); sort(v.begin(), v.end()); if (v[1] == v[2]) continue; int a = v[0], b = v[1], c = v[2], d = v[3]; int ans = get_ans(a, b, c, d); int ans1 = minimum_jumps(a, b, c, d); if (ans != ans1) { cout << nn << endl; for (auto i : hh) cout << i << " "; cout << endl << a << " " << b << " " << c << " " << d << endl; cout << ans << " " << ans1 << endl; return 0; } } } return 0; } #endif
#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...