Submission #1342754

#TimeUsernameProblemLanguageResultExecution timeMemory
1342754valerianToy (CEOI24_toy)C++20
0 / 100
5 ms8516 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define Valerian void
#define Valerian_or_Habil ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

inline ll enc(int r, int c) {
    return (1LL * r << 32) | c;
}

Valerian solve() {
    int n;
    cin >> n;

    int sl, sc, el, ec;
    cin >> sl >> sc >> el >> ec;

    vector<int> l(n + 2);
    for (int i = 1; i <= n; i++) cin >> l[i];

    if (sl == el && sc == ec) {
        cout << 0 << endl;
        return;
    }

    unordered_map<ll, int> dist;
    dist.reserve(1 << 20);

    queue<pair<int,int>> q;

    q.push({sl, sc});
    dist[enc(sl, sc)] = 0;

    while (!q.empty()) {
        pair<int,int> cur = q.front(); q.pop();
        int r = cur.first;
        int c = cur.second;

        int d = dist[enc(r, c)];

        if (r == el && c == ec) {
            cout << d << endl;
            return;
        }

        if (c > 1) {
            ll key = enc(r, c - 1);
            if (!dist.count(key)) {
                dist[key] = d + 1;
                q.push({r, c - 1});
            }
        } else if (r > 1) {
            int nc = l[r - 1] + 1;
            ll key = enc(r - 1, nc);
            if (!dist.count(key)) {
                dist[key] = d + 1;
                q.push({r - 1, nc});
            }
        }

        if (c < l[r] + 1) {
            ll key = enc(r, c + 1);
            if (!dist.count(key)) {
                dist[key] = d + 1;
                q.push({r, c + 1});
            }
        } else if (r < n) {
            ll key = enc(r + 1, 1);
            if (!dist.count(key)) {
                dist[key] = d + 1;
                q.push({r + 1, 1});
            }
        }

        if (r > 1) {
            int nc = min(c, l[r - 1] + 1);
            ll key = enc(r - 1, nc);
            if (!dist.count(key)) {
                dist[key] = d + 1;
                q.push({r - 1, nc});
            }
        }

        if (r < n) {
            int nc = min(c, l[r + 1] + 1);
            ll key = enc(r + 1, nc);
            if (!dist.count(key)) {
                dist[key] = d + 1;
                q.push({r + 1, nc});
            }
        }
    }
}

int main() {
    Valerian_or_Habil;
    solve();
}
#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...