#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const int N = 5007;
const ull INF = numeric_limits<ull>::max();
int n;
int h[N];
ull d[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n - 1; ++j) {
d[i][j] = INF;
}
}
priority_queue<pair<ull, pair<int, int>>, vector<pair<ull, pair<int, int>>>, greater<pair<ull, pair<int, int>>>> q;
auto upd = [&](int i, int j, ll dd) {
if (dd < d[i][j]) {
d[i][j] = dd;
q.push({dd, {i, j}});
}
};
upd(0, n - 2, 0);
while (!q.empty()) {
auto z = q.top();
q.pop();
auto [i, j] = z.second;
if (d[i][j] != z.first) {
continue;
}
if (i == j || i - 1 == j) {
cout << d[i][j] << endl;
return 0;
}
if (h[i] == h[j]) {
if (j > 0) {
upd(i, j - 1, d[i][j]);
}
upd(j, i, d[i][j]);
}
if (h[i] == h[j + 1]) {
if (j < n - 1) {
upd(i, j + 1, d[i][j]);
}
upd(j + 1, i, d[i][j]);
}
for (int ii : {i - 1, i + 1}) {
if (ii < 0 || n <= ii) {
continue;
}
if (h[i] < h[ii]) {
if (h[i] < h[j]) {
if (h[ii] - h[i] <= h[j] - h[i]) {
upd(ii, j, d[i][j] + h[ii] - h[i]);
} else {
upd(j, min(ii, i), d[i][j] + h[j] - h[i]);
}
} else {
if (h[ii] - h[i] <= h[j + 1] - h[i]) {
upd(ii, j, d[i][j] + h[ii] - h[i]);
} else {
upd(j + 1, min(ii, i), d[i][j] + h[j + 1] - h[i]);
}
}
}
if (h[i] > h[ii]) {
if (h[i] > h[j]) {
if (-h[ii] + h[i] <= -h[j] + h[i]) {
upd(ii, j, d[i][j] - h[ii] + h[i]);
} else {
upd(j, min(ii, i), d[i][j] - h[j] + h[i]);
}
} else {
if (-h[ii] + h[i] <= -h[j + 1] + h[i]) {
upd(ii, j, d[i][j] - h[ii] + h[i]);
} else {
upd(j + 1, min(ii, i), d[i][j] - h[j + 1] + h[i]);
}
}
}
}
}
cout << "NO" << endl;
return 0;
}
Compilation message
climbers.cpp: In lambda function:
climbers.cpp:33:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long int'} and 'ull' {aka 'long unsigned int'} [-Wsign-compare]
33 | if (dd < d[i][j]) {
| ~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
2124 KB |
Output is correct |
3 |
Correct |
6 ms |
12108 KB |
Output is correct |
4 |
Correct |
39 ms |
82716 KB |
Output is correct |
5 |
Correct |
110 ms |
196308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Incorrect |
3 ms |
3148 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
2252 KB |
Output isn't correct |
3 |
Incorrect |
46 ms |
12208 KB |
Output isn't correct |
4 |
Incorrect |
379 ms |
83440 KB |
Output isn't correct |
5 |
Incorrect |
465 ms |
83968 KB |
Output isn't correct |
6 |
Incorrect |
520 ms |
145748 KB |
Output isn't correct |
7 |
Incorrect |
616 ms |
142656 KB |
Output isn't correct |
8 |
Incorrect |
719 ms |
197516 KB |
Output isn't correct |
9 |
Execution timed out |
1102 ms |
198484 KB |
Time limit exceeded |
10 |
Execution timed out |
1088 ms |
197416 KB |
Time limit exceeded |