Submission #1262195

#TimeUsernameProblemLanguageResultExecution timeMemory
1262195kikitop1ggText editor (CEOI24_editor)C++17
5 / 100
4109 ms881816 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]);
        }
    }
    map<pp, vector<pair<pp, ll>>> g;
    map<pp, ll> dists;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < lines[i].size(); j++) {
            ll x = i, y = lines[i][j];
            dists[{x, y}] = inf;

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

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

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

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

    cout << dists[{ex, ey}] << '\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...