#include <bits/stdc++.h>
using namespace std;
const int NMAX = 5000;
const long long INF = 1e18;
priority_queue <pair<long long, pair<int, int>>> pq;
long long modul( long long x ) {return x < 0 ? -x : x;}
bool between( int a, int b, int c ) {return ( a <= b && b <= c ) || ( a >= b && b >= c );}
long long dist[NMAX + 1][NMAX + 1];
void update( int i, int j, long long d ) {
if ( d < dist[i][j] ) {
dist[i][j] = d;
pq.push( { -d, { i, j } } );
}
}
int main() {
int n;
cin >> n;
vector <int> v;
for ( int i = 1, x; i <= n; i ++ ) {
cin >> x;
v.push_back( x ) ;
while ( v.size() >= 3 && between( v[v.size() - 3], v[v.size() - 2], v[v.size() - 1] ) ) v.pop_back();
}
n = v.size();
for ( int i = 0; i <= n - 1; i ++ ) {
for ( int j = 0; j <= n - 2; j ++ ) {
dist[i][j] = INF;
}
}
dist[0][n - 2] = 0;
pq.push( { 0, {0, n - 2} } );
while ( !pq.empty() ) {
auto p = pq.top();
int i = p.second.first, j = p.second.second, d = dist[i][j];
if ( -p.first == d ) {
if ( i == j || i == j + 1 ) {
cout << d << '\n';
return 0;
}
if ( i + 1 < n && between( v[j], v[i + 1], v[j + 1] ) ) {
update( i + 1, j, d + modul( v[i] - v[i + 1] ) );
}
if ( i - 1 >= 0 && between( v[j], v[i - 1], v[j + 1] ) ) {
update( i - 1, j, d + modul( v[i] - v[i - 1] ) );
}
if ( i + 1 < n && between( v[i], v[j], v[i + 1] ) ) {
update( j, i, d + modul( v[i] - v[j] ) );
}
if ( i - 1 >= 0 && between( v[i], v[j], v[i - 1] ) ) {
update( j, i - 1, d + modul( v[i] - v[j] ) );
}
if ( i + 1 < n && between( v[i], v[j + 1], v[i + 1] ) ) {
update( j + 1, i, d + modul( v[i] - v[j + 1] ) );
}
if ( i - 1 >= 0 && between( v[i], v[j + 1], v[i - 1] ) ) {
update( j + 1, i - 1, d + modul( v[i] - v[j + 1] ) );
}
}
pq.pop();
}
cout << "NO\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
12636 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
39512 KB |
Output isn't correct |
4 |
Incorrect |
18 ms |
119528 KB |
Output isn't correct |
5 |
Incorrect |
32 ms |
195924 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
14684 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
18780 KB |
Output isn't correct |
4 |
Incorrect |
10 ms |
76380 KB |
Output isn't correct |
5 |
Incorrect |
18 ms |
62044 KB |
Output isn't correct |
6 |
Incorrect |
19 ms |
98904 KB |
Output isn't correct |
7 |
Incorrect |
41 ms |
76460 KB |
Output isn't correct |
8 |
Incorrect |
23 ms |
127768 KB |
Output isn't correct |
9 |
Incorrect |
19 ms |
123484 KB |
Output isn't correct |
10 |
Incorrect |
12 ms |
96984 KB |
Output isn't correct |