Submission #856915

# Submission time Handle Problem Language Result Execution time Memory
856915 2023-10-04T20:19:44 Z Tudy006 Climbers (RMI18_climbers) C++14
5 / 100
53 ms 220240 KB
#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, 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; 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 - 1 >= 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 && 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] ) );
        }

    }
    cout << "NO\n";
    return 0;
}
/**
         I
   /\    _  /\     _
  /  \  / \/  \   / \
 /    \/       \_/   \
/                     \
**/
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 6744 KB Output isn't correct
2 Incorrect 2 ms 15964 KB Output isn't correct
3 Incorrect 6 ms 43532 KB Output isn't correct
4 Incorrect 15 ms 121436 KB Output isn't correct
5 Incorrect 53 ms 220240 KB Output isn't correct
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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 78424 KB Output isn't correct
5 Incorrect 22 ms 70424 KB Output isn't correct
6 Incorrect 45 ms 112092 KB Output isn't correct
7 Incorrect 48 ms 84576 KB Output isn't correct
8 Incorrect 19 ms 132444 KB Output isn't correct
9 Incorrect 48 ms 138840 KB Output isn't correct
10 Incorrect 13 ms 98908 KB Output isn't correct