답안 #856917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856917 2023-10-04T20:41:56 Z Tudy006 Climbers (RMI18_climbers) C++14
55 / 100
88 ms 195932 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][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;
        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, 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;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 6 ms 39260 KB Output is correct
4 Incorrect 18 ms 119612 KB Output isn't correct
5 Incorrect 28 ms 195932 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 62 ms 39516 KB Output is correct
4 Incorrect 88 ms 113432 KB Output isn't correct
5 Incorrect 48 ms 113440 KB Output isn't correct
6 Incorrect 26 ms 156504 KB Output isn't correct
7 Incorrect 43 ms 150360 KB Output isn't correct
8 Incorrect 30 ms 187224 KB Output isn't correct
9 Incorrect 32 ms 189288 KB Output isn't correct
10 Incorrect 56 ms 187220 KB Output isn't correct