This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <math.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <iomanip>
#include <random>
#include <chrono>
#include <cassert>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define US freopen(".in", "r", stdin); freopen("j.out", "w", stdout);
ll gcd(ll a, ll b)
{
if (a == 0 || b == 0) {
return max(a, b);
}
if (a <= b) {
return gcd(a, b % a);
}
else {
return gcd(a % b, b);
}
}
ll lcm(ll a, ll b) {
return (a / gcd(a, b)) * b;
}
const int N = 1000005;
const ll oo = 100000000000000000, MOD = 1000000007;
int l[N];
void solve() {
int n;
ll xs, ys, xe, ye;
cin >> n >> xs >> ys >> xe >> ye;
for (int i = 1; i <= n; ++i) {
cin >> l[i];
}
if (xs == xe && ys == ye) {
cout << 0 << endl;
return;
}
ll ans = abs(xs - xe) + abs(ys - ye);
if (xs == xe) {
cout << min(min(ans, l[xs] + 1 - ys + 1 + 1 + ye - 1),ys+l[xs]+1-ye) << endl;
}
else {
if (xe == n) {
cout << min(ans,1LL) << endl;
}
else {
cout << min(ans, 1 + l[xe] + 1 - ye) << endl;
}
}
return;
if (xs == xe) {
cout << min(ans, min(ys + 1 + l[xe] + 1 - ye, l[xs] + 1 - ys + 1 + 1 + ye - 1)) << endl;
}
else {
if (xs > xe) {
ans = min(ans, ys + xs - 1 - xe + l[xe] + 1 - ye);
ans = min(ans, ys - 1 + xs - xe + ye - 1);
ans = min(ans, l[xs] + 1 - ys + xs - xe + l[xe] + 1 - ye);
ans = min(ans, l[xs] + 1 - ys + 1 + xs + 1 - xe + ye - 1);
}
else {
ans = min(ans, ys + xe - (xs - 1) + l[xe] + 1 - ye);
ans = min(ans, ys - 1 + xe - xs + ye - 1);
ans = min(ans, l[xs] + 1 - ys + xe - xs + l[xe] + 1 - ye);
ans = min(ans, l[xs] + 1 - ys + 1 + xe - (xs + 1) + ye - 1);
}
cout << ans << endl;
}
return;
if (xs == n) {
cout << min(min(abs(xs - xe) + l[xe] + 1 - ye, abs(xs - xe) + ye - 1),ans) << endl;
}
else {
if (xs == 1) {
if (xs == xe) {
cout << min(ans, l[xs] + 1 - ys + 1 + 1 + ye - 1) << endl;
}
else {
ans = min(ans, ys - 1 + xe - xs + ye - 1);
ans = min(ans, l[xs] + 1 - ys + xe - xs + l[xe] + 1 - ye);
cout << ans << endl;
}
}
else {
if (xs == xe) {
cout << min(ans, min(ys + 1 + l[xe] + 1 - ye, l[xs] + 1 - ys + 1 + 1 + ye - 1)) << endl;
}
else {
if (xs > xe) {
ans = min(ans, ys + xs - 1 - xe + l[xe] + 1 - ye);
ans = min(ans, ys - 1 + xs - xe + ye - 1);
ans = min(ans, l[xs] + 1 - ys + xs - xe + l[xe] + 1 - ye);
ans = min(ans, l[xs] + 1 - ys + 1 + xs + 1 - xe + ye - 1);
}
else {
ans = min(ans, ys + xe - (xs - 1) + l[xe] + 1 - ye);
ans = min(ans, ys - 1 + xe - xs + ye - 1);
ans = min(ans, l[xs] + 1 - ys + xe - xs + l[xe] + 1 - ye);
ans = min(ans, l[xs] + 1 - ys + 1 + xe - (xs + 1) + ye - 1);
}
cout << ans << endl;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//US
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
# | 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... |