Submission #696419

#TimeUsernameProblemLanguageResultExecution timeMemory
696419jhwest2Climbers (RMI18_climbers)C++14
100 / 100
222 ms431600 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef array<int, 3> P; const int N = 5050; const ll INF = 1e18 + 10; int n, a[N], p; ll dp[2 * N][2 * N]; bool inq[2 * N][2 * N]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; if (p && a[p - 1] == x) continue; while (p >= 2 && (a[p - 2] < a[p - 1]) == (a[p - 1] < x)) --p; a[p++] = x; } n = p; for (int i = 0; i <= 2 * n - 2; i++) for (int j = i; j <= 2 * n - 2; j++) dp[i][j] = INF; dp[0][2 * n - 2] = 0; auto go = [&](int x, int y, int tx, int ty) { int p = (x & 1) ? a[y / 2] : a[x / 2]; int nx = (x & 1) ? (tx ? x + 1 : x - 1) : (tx ? x + 2 : x - 2); int ny = (y & 1) ? (ty ? y - 1 : y + 1) : (ty ? y - 2 : y + 2); if (nx < 0 || ny < 0 || nx > 2 * n - 2 || ny > 2 * n - 2) return P{ -1, -1, 0 }; bool dx = (tx ? a[nx / 2 - 1] : a[nx / 2 + 1]) < a[nx / 2]; // true if climbing bool dy = (ty ? a[ny / 2 + 1] : a[ny / 2 - 1]) < a[ny / 2]; if (dx != dy) return P{ -1, -1, 0 }; if (a[nx / 2] == a[ny / 2]) return P{ nx, ny, abs(a[nx / 2] - p) }; if (dx == (a[nx / 2] < a[ny / 2])) return P{ nx, ty ? ny + 1 : ny - 1, abs(a[nx / 2] - p) }; else return P{ tx ? nx - 1 : nx + 1, ny, abs(a[ny / 2] - p) }; }; priority_queue<array<ll, 3>> pq; pq.push({ 0, 0, 2 * n - 2 }); while (!pq.empty()) { auto [d, l, r] = pq.top(); pq.pop(); d = -d; if (l == r) break; for (int tx = 0; tx <= 1; tx++) for (int ty = 0; ty <= 1; ty++) { auto [x, y, d] = go(l, r, tx, ty); if (x != -1) { if (dp[x][y] > dp[l][r] + d) { dp[x][y] = dp[l][r] + d; pq.push({ -dp[x][y], x, y }); } } } } ll ans = INF; for (int i = 0; i < n; i++) ans = min(ans, dp[2 * i][2 * i]); cout << ans; }

Compilation message (stderr)

climbers.cpp: In function 'int main()':
climbers.cpp:56:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |         auto [d, l, r] = pq.top(); pq.pop();
      |              ^
climbers.cpp:62:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   62 |             auto [x, y, d] = go(l, r, tx, ty);
      |                  ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...