#include <bits/stdc++.h>
using namespace std;
// typedefs
// #define int long long int
typedef long long ll;
typedef long double ld;
using str = string;
// typedef double ld;
// pairs
template <class T> using P = pair<T, T>;
using pi = P<int>;
using pl = P<ll>;
using pd = P<ld>;
using weg = pair<int, pi>; // weighted edge
#define mp make_pair
#define f first
#define s second
// vectors
template <class T> using V = vector<T>;
using vi = V<int>;
using vl = V<ll>;
using vd = V<ld>;
using vb = V<bool>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
using vvi = V<vi>;
using vweg = V<weg>;
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int ilb(V<T>& a, const T& b) {return int(lb(all(a), b) - (a).begin());}
template<class T> int iub(V<T>& a, const T& b) {return int(ub(all(a), b) - (a).begin());}
// queues
template<class T> using prq = priority_queue<T, V<T>>;
// macros
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (a); i >= (b); --i)
#define rep(a) FOR (_, 0, a)
#define each(a, x) for (auto& a: x)
#define trace(x) each(a, (x)) {cout << a << " ";}
#define tracel(x) each(a, (x)) {cout << a << "\n";}
void solve() {
ll n; cin >> n;
ll Sr, Sc, Er, Ec; cin >> Sr >> Sc >> Er >> Ec;
vl lengths(n + 1, -1); FOR(i, 1, n + 1) {cin >> lengths[i];}
ll ans = 0;
if (n == 2) {
// distance from r1, c1 to r2, c2 may be different from distance r2, c2, to r1, c1
// it can never be optimal to go up or down a line to reach a position on the current line >= current column
// however if a position is before your current column, it may be optimal to go up to a shorter line, then go back down
// there is no faster way to get to a certain y value than going down/up directly
// there is some form of dp here
// potentially for each row, col, store the fastest way to reach a certain col, irrespective of row?
ans = ll(pow(10, 18));
if (Sr == 1) {
ll l2pos = min(Sc, lengths[2]);
if (Er == 1) {
ans = min(ans, abs(Sc - Ec));
ans = min(ans, abs(l2pos - Ec) + 2);
}
if (Er == 2) {
ans = min(ans, abs(l2pos - Ec) + 1);
}
}
ans = ll(pow(10, 18));
if (Sr == 1) {
ll l2pos = min(Sc, lengths[2]);
if (Er == 1) {
ans = min(ans, abs(Sc - Ec));
ans = min(ans, abs(l2pos - Ec) + 2);
}
if (Er == 2) {
ans = min(ans, abs(l2pos - Ec) + 1);
}
}
if (Sr == 2) {
ll l2pos = min(Sc, lengths[1]);
if (Er == 2) {
ans = min(ans, abs(Sc - Ec));
ans = min(ans, abs(l2pos - Ec) + 2);
}
if (Er == 1) {
ans = min(ans, abs(l2pos - Ec) + 1);
}
}
}
cout << ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
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... |