#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
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 time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
11 ms |
24164 KB |
Output is correct |
4 |
Correct |
68 ms |
165560 KB |
Output is correct |
5 |
Correct |
205 ms |
431600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
3 ms |
5972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
3796 KB |
Output is correct |
3 |
Correct |
30 ms |
22624 KB |
Output is correct |
4 |
Correct |
105 ms |
152232 KB |
Output is correct |
5 |
Correct |
111 ms |
151044 KB |
Output is correct |
6 |
Correct |
152 ms |
275744 KB |
Output is correct |
7 |
Correct |
150 ms |
260728 KB |
Output is correct |
8 |
Correct |
202 ms |
390592 KB |
Output is correct |
9 |
Correct |
222 ms |
397800 KB |
Output is correct |
10 |
Correct |
193 ms |
389096 KB |
Output is correct |