#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];
bool viz[NMAX + 1][NMAX + 1];
void update( int i, int j, int 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 - 1; j ++ ) {
dist[i][j] = INF;
}
}
dist[0][n - 2] = 0;
pq.push( { 0, {0, n - 2} } );
while ( !pq.empty() ) {
auto p = pq.top();
pq.pop();
int i = p.second.first, j = p.second.second, d = dist[i][j];
if ( i == j || i == j + 1 ) {
cout << dist[i][j] << '\n';
return 0;
}
if ( viz[i][j] ) continue;
viz[i][j] = true;
if ( j > 0 && v[i] == v[j] ) update( i, j - 1, d );
if ( j < n - 1 && v[i] == v[j + 1] ) update( i, j + 1, d );
if ( i > 0 && ( between( v[i], v[i - 1], v[j] ) || between( v[i], 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[i + 1], v[j] ) || between( v[i], 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 && j + 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 && j + 1 < n && between( v[i], v[j + 1], v[i - 1] ) ) {
update( j + 1, i - 1, d + modul( v[i] - v[j + 1] ) );
}
}
cout << "NO\n";
return 0;
}
/**
I
/\ _ /\ _
/ \ / \/ \ / \
/ \/ \_/ \
/ \
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
15964 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
43352 KB |
Output isn't correct |
4 |
Incorrect |
15 ms |
119388 KB |
Output isn't correct |
5 |
Incorrect |
53 ms |
220248 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
9052 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
18012 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
78428 KB |
Output isn't correct |
5 |
Incorrect |
23 ms |
70512 KB |
Output isn't correct |
6 |
Incorrect |
49 ms |
112212 KB |
Output isn't correct |
7 |
Incorrect |
50 ms |
84560 KB |
Output isn't correct |
8 |
Incorrect |
19 ms |
132444 KB |
Output isn't correct |
9 |
Incorrect |
51 ms |
138836 KB |
Output isn't correct |
10 |
Incorrect |
13 ms |
98904 KB |
Output isn't correct |