Submission #856914

#TimeUsernameProblemLanguageResultExecution timeMemory
856914Tudy006Climbers (RMI18_climbers)C++14
5 / 100
53 ms220248 KiB
#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 /\ _ /\ _ / \ / \/ \ / \ / \/ \_/ \ / \ **/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...