Submission #1262207

#TimeUsernameProblemLanguageResultExecution timeMemory
1262207kikitop1ggText editor (CEOI24_editor)C++17
16 / 100
2608 ms851036 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<pair<ll, ll>>
#define vpp vector<pair<ll, pair<ll, ll>>>
#define pp pair<ll, ll>
#define qi queue<ll>
#define qp queue<pp>
#define pqi priority_queue<ll>
#define pqp priority_queue<pp>
#define mi map<ll, ll>
#define mpi map<pp, ll>
#define mip map<ll, pp>
#define mp map<pp, pp>
#define mb map<ll, bool>
#define si set<ll>
#define sp set<pp>
#define sc set<char>
#define mod 1000000007
#define inf 1000000000000000000
#define all(x) (x).begin(), (x).end()
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n;
    cin >> n;
    ll sx, sy, ex, ey;
    cin >> sx >> sy >> ex >> ey;
    sx--; sy--; ex--; ey--;
    vi a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vvi lines(n);    
    for(int i = 0; i < n; i++) {
        lines[i].push_back(0);
        ll mn = min(sy, ey), mx = max(sy, ey);
        if(lines[i].back() != mn && a[i] >= mn) {
            lines[i].push_back(mn);
        }
        if(lines[i].back() != mx && a[i] >= mx) {
            lines[i].push_back(mx);
        }
        if(lines[i].back() != a[i]) {
            lines[i].push_back(a[i]);
        }
    }
    struct PairHash {
        size_t operator()(const pp &p) const noexcept {
            return ((uint64_t)p.first << 32) ^ (uint64_t)p.second;
        }
    };
    unordered_map<pp, ll, PairHash> coor;    
    vp coor2;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < lines[i].size(); j++) {
            ll x = i, y = lines[i][j];
            coor[{x, y}] = coor2.size();
            coor2.push_back({x, y});
        }
    }
    vector<vp> g(coor2.size());
    vi dists(coor2.size(), inf);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < lines[i].size(); j++) {
            ll x = i, y = lines[i][j];
            ll ind = coor[{x, y}];

            ll newX = x, newY = y, cost = 1;
            if(j > 0) {
                newX = x;
                newY = lines[i][j - 1];
                cost = abs(y - newY);
                ll nextInd = coor[{newX, newY}];
                g[ind].push_back({nextInd, cost});
            }
            else if(i > 0) {
                newX = x - 1;
                newY = lines[newX].back();
                cost = 1;
                ll nextInd = coor[{newX, newY}];
                g[ind].push_back({nextInd, cost});
            }

            if(j + 1 < lines[i].size()) {
                newX = x;
                newY = lines[i][j + 1];
                cost = abs(y - newY);
                ll nextInd = coor[{newX, newY}];
                g[ind].push_back({nextInd, cost});
            }
            else if(i + 1 < n) {
                newX = x + 1;
                newY = 0;
                cost = 1;
                ll nextInd = coor[{newX, newY}];
                g[ind].push_back({nextInd, cost});
            }

            if(i > 0) {
                newX = x - 1;
                newY = min(y, a[newX]);
                cost = 1;
                ll nextInd = coor[{newX, newY}];
                g[ind].push_back({nextInd, cost});
            }

            if(i + 1 < n) {
                newX = x + 1;
                newY = min(y, a[newX]);
                cost = 1;
                ll nextInd = coor[{newX, newY}];
                g[ind].push_back({nextInd, cost});
            }
        }
    }
    ll startInd = coor[{sx, sy}], endInd = coor[{ex, ey}];
    dists[startInd] = 0;
    
    priority_queue<pp> q;
    q.push({0, startInd});
    vp dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
    while(q.size()) {
        ll cur = -q.top().first, v = q.top().second;
        q.pop();
        if(dists[v] < cur) continue;
        
        for(auto[x, cost] : g[v]) {
            if(dists[x] > cur + cost) {
                dists[x] = cur + cost;
                q.push({-(cur + cost), x});
            }
        }
    }

    cout << dists[endInd] << '\n';
    return 0;
}
#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...