이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 1e6 + 10;
int l[N];
set <int> ms;
priority_queue <pair<int, pair<int, int>>> Q;
map <pair<int, int>, int> mp;
void check(pair<int, int> v, int d) {
if (mp.find(v) == mp.end() || mp[v] > d) {
mp[v] = d;
Q.push({ -d, v });
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
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<int, 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... |