# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1053360 | mindiyak | Text editor (CEOI24_editor) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <cmath>
#include <numeric>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<pi>> vvpi;
typedef vector<ld> vd;
typedef vector<long long> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (ll i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define len(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define nl endl
#define S second
#define lb lower_bound
#define ub upper_bound
#define aint(x) x.begin(), x.end()
#define raint(x) x.rbegin(), x.rend()
#define ins insert
const int M = 1e9+7;
void init(string name)
{
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
}
ll ans = -1;
pi snode,enode;
int n;
vi arr;
int dis(pi a,pi b){
int val = abs(a.F-b.F) + abs(a.S-b.S);
val = min(val,a.F+(arr[0]-b.F)+ abs(a.S-b.S) -1);
val = min(val,b.F+(arr[0]-a.F)+ abs(a.S-b.S) -1);
return val;
}
void dfs(pi pos,int cnt){
if(ans != -1)return;
// cout << pos.F << "," << pos.S << " " << cnt << endl;
if(pos == enode){
ans = cnt;
return;
}
priority_queue<pair<int,pi>> pq;
//left
pi a = pos;
a.F -= 1;
if(a.F < 0){
if(a.S == 0){
a = {0,0};
}else{
a = {arr[a.S-1],a.S-1};
}
}
pq.push({-dis(a,enode),a});
// cout << "L " << a.F << "," << a.S << endl;
//right
a = pos;
a.F += 1;
if(a.F > arr[a.S]){
if(a.S == n-1){
a = {n-1,0};
}else{
a = {0,a.S+1};
}
}
pq.push({-dis(a,enode),a});
// cout << "R " << a.F << "," << a.S << endl;
//down
a = pos;
a.S = min(n-1,a.S+1);
if(a.F > arr[a.S]){
a = {arr[a.S],a.S};
}
pq.push({-dis(a,enode),a});
// cout << "D " << a.F << "," << a.S << endl;
//up
a = pos;
a.S = max(0,a.S-1);
if(a.F > arr[a.S]){
a = {arr[a.S],a.S};
}
pq.push({-dis(a,enode),a});
// cout << "U " << a.F << "," << a.S << endl;
while(!pq.empty()){
auto b = pq.top();pq.pop();
// cout << b.F << " " << b.S.F << "," << b.S.S << endl;
if(dis(a,pos) == 0)continue;
dfs(b.S,cnt+1);
}
}
void solve()
{
cin >> n;
if(n==1)return 0;
int a,b,c,d;
cin >> a >> b;
cin >> c >> d;
a--;b--;c--;d--;
snode = {b,a};enode = {d,c};
if(n == 2){
cout << dis(snode,enode) << endl;
return;
}
arr = vi(n);
FOR(i,0,n)cin >> arr[i];
dfs(snode,0);
cout << ans << endl;
}
int main()
{
fastIO();
// init("test");
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}