제출 #1137620

#제출 시각아이디문제언어결과실행 시간메모리
1137620Ghulam_JunaidText editor (CEOI24_editor)C++20
45 / 100
4106 ms278760 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10; // 3e6
int n, len[N], sl, sc, el, ec;

map<pair<int, int>, int> dist;
set<pair<int, pair<int, int>>> st;

int distance(int x, int y){
    if (dist.find({x, y}) == dist.end())
        dist[{x, y}] = 2e9;
    return dist[{x, y}];
}

void upd(int x, int y, int nd){
    if (distance(x, y) > nd){
        st.erase({dist[{x, y}], {x, y}});
        dist[{x, y}] = nd;
        st.insert({dist[{x, y}], {x, y}});
    }
}

void dijkstra(int x, int y){
    upd(x, y, 0);
    while (!st.empty()){
        auto [d, P] = *st.begin();
        auto [x, y] = P;
        st.erase(st.begin());

        upd(x, 0, d + y);
        upd(x, len[x], d + len[x] - y);
        if (x - 1 > 0){
            int up = min(len[x - 1], y);
            upd(x - 1, up, d + 1);
        }
        if (x + 1 <= n){
            int down = min(len[x + 1], y);
            upd(x + 1, down, d + 1);
        }
        if (y == 0)
            if (x - 1 > 0)
                upd(x - 1, len[x - 1], d + 1);
        if (y == len[x])
            if (x + 1 <= n)
                upd(x + 1, 0, d + 1);
    }
}

int main(){
    cin >> n >> sl >> sc >> el >> ec;
    sc--, ec--;
    for (int i = 1; i <= n; i ++)
        cin >> len[i];

    dijkstra(sl, sc);
    
    int ans = 2e9;
    for (auto [P, d] : dist){
        auto [x, y] = P;
        if (x != el) continue;
        ans = min(ans, d + abs(y - ec));
    }
    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...