Submission #856926

# Submission time Handle Problem Language Result Execution time Memory
856926 2023-10-04T20:54:37 Z Tudy006 Climbers (RMI18_climbers) C++14
100 / 100
152 ms 195924 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;

const int NMAX = 5000;
const long long INF = 1e18;

priority_queue <pair<long long, pair<int, int>>> pq;

inline int modul( int x ) {return x < 0 ? -x : x;}
inline bool between( int a, int b, int c ) {return ( a <= b && b <= c ) || ( a >= b && b >= c );}

long long dist[NMAX][NMAX - 1];

inline 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;
        while ( v.size() >= 2 && between( v[v.size() - 2], v[v.size() - 1], x ) ) v.pop_back();
        v.push_back( x );
    }


    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;
        long long 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 Correct 1 ms 4596 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 5 ms 39476 KB Output is correct
4 Correct 19 ms 119388 KB Output is correct
5 Correct 48 ms 195924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 58 ms 39500 KB Output is correct
4 Correct 103 ms 113440 KB Output is correct
5 Correct 133 ms 113240 KB Output is correct
6 Correct 135 ms 156508 KB Output is correct
7 Correct 143 ms 150420 KB Output is correct
8 Correct 117 ms 187228 KB Output is correct
9 Correct 151 ms 189440 KB Output is correct
10 Correct 152 ms 187380 KB Output is correct