#include <bits/stdc++.h>
#define nemeshay ios_base::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
#define int long long
#define sigma signed
#define pb push_back
#define fr first
#define sc second
#define pii pair<int, int>
using namespace std;
const int N = 1e6 + 2, inf = 1e18 + 7;
int a[N];
sigma main() {
int n, xs, ys, xf, yf, ans;
cin >> n >> xs >> ys >> xf >> yf;
if (!xf) {
cout << n - xs;
return 0;
}
for (int i = 0; i < n; i++) cin >> a[i];
ans = abs(xs - xf) + abs(ys - yf);
if (xs > 1) {
int cnt = 0;
cnt += ys;
cnt += abs(xs - 1 - xf) + abs(a[0] + 1 - yf);
ans = min(ans, cnt);
}
if (xs < n) {
int cnt = 0;
cnt += a[0] + 1 - ys + 1;
cnt += abs(xs + 1 - xf) + abs(1 - yf);
ans = min(ans, cnt);
}
if (xs > 1) {
int cnt = 1;
cnt += a[0] + 1 - ys + 1;
cnt += abs(xs - xf) + abs(1 - yf);
ans = min(ans, cnt);
}
if (xs < n) {
int cnt = 1;
if (xs == n - 1) cnt++;
else cnt += ys;
cnt += abs(xs - xf) + abs(a[0] + 1 - yf);
ans = min(ans, cnt);
}
cout << ans << endl;
}
# | 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... |