This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
typedef long long ll;
using namespace std;
const int N = 2e5 + 10;
int l[N];
set <int> ms;
struct CustomComparator {
bool operator()(const pair<ll, pair<int, int>>& x, const pair<ll, pair<int, int>>& y) {
if (x.first != y.first) {
return x.first < y.first;
}
return false;
}
};
priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>, CustomComparator> Q;
map <pair<int, int>, ll> mp;
void check(pair<int, int> v, ll d) {
if (mp.find(v) == mp.end() || mp[v] > d) {
mp[v] = d;
Q.push({ d, v });
}
}
int main() {
int n;
cin >> n;
int sl, sc, el, ec;
cin >> sl >> sc >> el >> ec;
ms.insert(1);
ms.insert(sc);
ms.insert(ec);
for (int i = 1; i <= n; i++) {
cin >> l[i];
l[i]++;
ms.insert(l[i]);
}
mp[{ sl, sc }] = 0;
Q.push({ 0, { sl, sc } });
while (!Q.empty()) {
pair<ll, pair<int, int>> v = Q.top(); Q.pop();
//cout << v.first << " " << v.second.first << " " << v.second.second << "\n";
ll d = v.first;
int r = v.second.first;
int c = v.second.second;
if (mp[{ r, c }] != d) continue;
if (c == l[r]) {
if (r != n) {
check({ r + 1, 1 }, d + 1);
}
}
else {
auto it = ms.upper_bound(c);
check({ r, *it }, *it - c + d);
}
if (c == 1) {
if (r != 1) {
check({ r - 1, l[r - 1] }, d + 1);
}
}
else {
auto it = ms.lower_bound(c); it--;
check({ r, *it }, c - *it + d);
}
if (r > 1) {
check({ r - 1, min(l[r - 1], c) }, d + 1);
}
if (r < n) {
check({ r + 1, min(l[r + 1], c) }, d + 1);
}
}
cout << mp[{ el, ec }] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |