Submission #336745

# Submission time Handle Problem Language Result Execution time Memory
336745 2020-12-16T16:48:28 Z Sorting Climbers (RMI18_climbers) C++17
5 / 100
111 ms 215916 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll N = 5000 + 3;
const ll INF = 1e18;

void build_edges(ll pos1, ll pos2, bool exact, vector<array<ll, 4>> &adj);

ll n, a[N];
ll dist[N][N][2];

ll dijkstra(ll pos1, ll pos2, bool exact){
    priority_queue<array<ll, 4>, vector<array<ll, 4>>, greater<array<ll, 4>>> pq;

    for(ll pos1 = 0; pos1 < n; ++pos1)
        for(ll pos2 = pos1; pos2 < n; ++pos2)
            for(ll exact = 0; exact <= 1; ++exact)
                dist[pos1][pos2][exact] = INF;

    dist[pos1][pos2][exact] = 0;
    pq.push({0, pos1, pos2, exact});

    vector<array<ll, 4>> adj;
    while(!pq.empty()){
        auto [d, pos1, pos2, exact] = pq.top();
        pq.pop();

        if(d != dist[pos1][pos2][exact]) continue;
        if(pos1 == pos2) return d;

        adj.clear();
        build_edges(pos1, pos2, exact, adj);
        for(auto [pos1_to, pos2_to, exact_to, cost]: adj){
            ll cand = d + cost;
            if(cand < dist[pos1_to][pos2_to][exact_to]){
                dist[pos1_to][pos2_to][exact_to] = cand;
                pq.push({cand, pos1_to, pos2_to, exact_to});
            } 
        }
    }

    return INF;
}

void move_dp(ll mv1, ll mv2, ll pos1, ll pos2, bool exact, ll x, vector<array<ll, 4>> &adj){
    //cout << mv1 << " " << mv2 << " " << pos1 << " " << pos2 << " " << exact <<  " " << x << endl;
    if(a[mv1] > x && x > a[mv2]) return;
    if(a[mv1] < x && x < a[mv2]) return;

    ll new_x;
    if(a[mv1] < x) new_x = max(a[mv1], a[mv2]);
    else new_x = min(a[mv1], a[mv2]);

    ll new_exact = (a[mv1] == new_x) ? 0 : 1;
    
    ll new_pos1 = (a[mv1] == new_x) ? mv1 : pos1;
    ll new_pos2 = (a[mv2] == new_x) ? mv2 : pos2;
    if(a[pos1] == x && a[mv1] != new_x){
        if(mv1 == pos1 + 1) new_pos1 = pos1;
        else new_pos1 = pos1 - 1;
    }
    if(a[pos2] == x && a[mv2] != new_x){
        if(mv2 == pos2 - 1) new_pos2 = pos2;
        else new_pos2 = pos2 + 1;
    }

    adj.push_back({new_pos1, new_pos2, new_exact, abs(x - new_x)});
    
    //cout << pos1 << " " << pos2 << " " << exact << " -> " << new_pos1 << " " << new_pos2 << " " << new_exact << " " << abs(x - new_x) << endl;
}

void build_edges(ll pos1, ll pos2, bool exact, vector<array<ll, 4>> &adj){
    ll x = !exact ? a[pos1] : a[pos2];
    bool e1 = (x == a[pos1]), e2 = (x == a[pos2]);
    vector<ll> v1, v2;

    if(e1){
        v1.push_back(pos1 + 1);
        if(pos1) v1.push_back(pos1 - 1);
    }
    else{
        v1.push_back(pos1 + 1);
        v1.push_back(pos1);
    }

    if(e2){
        v2.push_back(pos2 - 1);
        if(pos2 != n - 1) v2.push_back(pos2 + 1);
    }
    else{
        v2.push_back(pos2 - 1);
        v2.push_back(pos2);
    }

    move_dp(v1[0], v2[0], pos1, pos2, exact, x, adj);
    if(v2.size() == 2) move_dp(v1[0], v2[1], pos1, pos2, exact, x, adj);
    if(v1.size() == 2) move_dp(v1[1], v2[0], pos1, pos2, exact, x, adj);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(ll i = 0; i < n; ++i)
        cin >> a[i];

    vector<ll> v;
    v.push_back(a[0]);

    for(ll i = 1; i < n - 1; ++i){
        if(v.back() <= a[i] && a[i] <= a[i + 1])
            continue;
        if(v.back() >= a[i] && a[i] >= a[i + 1])
            continue;
        v.push_back(a[i]);
    }
    v.push_back(a[n - 1]);

    n = v.size();
    for(ll i = 0; i < n; ++i)
        a[i] = v[i];

    //build_edges(0, n - 1, 0);
    ll ans = dijkstra(0, n - 1, 0);

    if(ans == INF) cout << "NO\n";
    else cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 876 KB Output isn't correct
2 Incorrect 1 ms 2284 KB Output isn't correct
3 Incorrect 7 ms 12268 KB Output isn't correct
4 Incorrect 44 ms 82924 KB Output isn't correct
5 Incorrect 111 ms 215916 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 620 KB Output is correct
2 Incorrect 1 ms 620 KB Output isn't correct
3 Incorrect 1 ms 1132 KB Output isn't correct
4 Incorrect 1 ms 876 KB Output isn't correct
5 Incorrect 2 ms 3180 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 748 KB Output isn't correct
2 Incorrect 1 ms 2156 KB Output isn't correct
3 Incorrect 8 ms 11500 KB Output isn't correct
4 Incorrect 40 ms 76268 KB Output isn't correct
5 Incorrect 40 ms 75756 KB Output isn't correct
6 Incorrect 73 ms 138092 KB Output isn't correct
7 Incorrect 69 ms 130540 KB Output isn't correct
8 Incorrect 102 ms 195564 KB Output isn't correct
9 Incorrect 105 ms 199148 KB Output isn't correct
10 Incorrect 101 ms 194668 KB Output isn't correct