Submission #1208315

#TimeUsernameProblemLanguageResultExecution timeMemory
1208315HappyCapybaraText editor (CEOI24_editor)C++20
45 / 100
4117 ms721168 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

int h(int x, int y){
    return x*pow(10, 9)+y;
}

signed main(){
    cin.tie(0);
    iostream::sync_with_stdio(false);
    int n, sl, sc, el, ec;
    cin >> n >> sl >> sc >> el >> ec;
    sl--; sc--; el--; ec--;
    set<int> s;
    s.insert(0);
    s.insert(sc);
    s.insert(ec);
    vector<int> l(n);
    for (int i=0; i<n; i++){
        cin >> l[i];
        s.insert(l[i]);
    }
    vector<int> v;
    for (int x : s) v.push_back(x);
    int vs = v.size();
    unordered_map<int,vector<pair<int,int>>> g;
    for (int i=0; i<n; i++){
        for (int j=0; j<vs; j++){
            if (v[j] > l[i]) break;
            if (j > 0) g[h(i, v[j])].push_back({h(i, v[j-1]), v[j]-v[j-1]});
            else if (i > 0) g[h(i, v[j])].push_back({h(i-1, l[i-1]), 1});
            if (j < vs-1 && v[j+1] <= l[i]) g[h(i, v[j])].push_back({h(i, v[j+1]), v[j+1]-v[j]});
            else if (i < n-1) g[h(i, v[j])].push_back({h(i+1, 0), 1});
            if (i > 0){
                if (v[j] <= l[i-1]) g[h(i, v[j])].push_back({h(i-1, v[j]), 1});
                else g[h(i, v[j])].push_back({h(i-1, l[i-1]), 1});
            }
            if (i < n-1){
                if (v[j] <= l[i+1]) g[h(i, v[j])].push_back({h(i+1, v[j]), 1});
                else g[h(i, v[j])].push_back({h(i+1, l[i+1]), 1});
            }
        }
    }
    unordered_set<int> seen;
    priority_queue<pair<int,int>> pq;
    pq.push({0, h(sl, sc)});
    while (!pq.empty()){
        int cur = pq.top().second, d = -pq.top().first;
        pq.pop();
        if (seen.find(cur) != seen.end()) continue;
        seen.insert(cur);
        if (cur == h(el, ec)){
            cout << d << endl;
            return 0;
        }
        for (pair<int,int> next : g[cur]) pq.push({-(d+next.second), next.first});
    }
}
#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...