This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//-------------dilanyan------------\\
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
//------------------Kargpefines--------------------\\
#define ll long long
#define pb push_back
#define all(u) (u).begin(), (u).end()
#define pqueue priority_queue
#define upper upper_bound
#define lower lower_bound
#define umap unordered_map
#define uset unordered_set
void KarginSet(string name = "") {
ios_base::sync_with_stdio(false); cin.tie(NULL);
if (name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
//-------------------KarginConstants------------------\\
const ll mod = 1e9 + 7;
const ll inf = 2e9;
//-------------------KarginCode-----------------------\\
const int N = 2000005, M = 1000005;
ll l[N];
vector<pair<int, ll>> g[N];
void KarginSolve() {
ll n, sl, sc, el, ec;
cin >> n >> sl >> sc >> el >> ec;
sc--, ec--;
for (int i = 1;i <= n;i++) cin >> l[i];
for (int i = 2;i <= n;i++) {
g[i].pb({ i - 1,1 }), g[i - 1].pb({ i,1 });
}
stack<int> st;
for (int i = 1; i <= n;i++) {
while (!st.empty() && l[st.top()] > l[i]) st.pop();
if (!st.empty()) {
g[n + i].pb({ n + st.top(),i - st.top() });
}
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = n;i >= 1;i--) {
while (!st.empty() && l[st.top()] > l[i]) st.pop();
if (!st.empty()) {
g[n + i].pb({ n + st.top(),st.top() - i });
}
st.push(i);
}
for (int i = 1;i < n;i++) {
g[i + 1].pb({ n + i,1 });
g[n + i].pb({ i + 1,1 });
}
pqueue<pair<ll, int>> pq;
vector<ll> d(2 * n + 3, inf);
vector<bool> vis(2 * n + 3, false);
g[0].pb({ sl,sc });
g[sl].pb({ 0,sc });
g[0].pb({ sl + n,l[sl] - sc });
g[sl + n].pb({ 0,l[sl] - sc });
for (int i = sl + 1;i <= n;i++) {
if (l[i] <= sc) {
g[0].pb({ i + n,i - sl });
g[i + n].pb({ 0,i - sl + sc - l[i] });
break;
}
g[0].pb({ n + i,l[i] - sc + i - sl });
g[n + i].pb({ 0,l[i] - sc + i - sl });
}
for (int i = sl - 1;i >= 1;i--) {
if (l[i] <= sc) {
g[0].pb({ n + i,sl - i });
g[n + i].pb({ 0,sl - i + sc - l[i] });
break;
}
g[0].pb({ n + i,l[i] - sc + sl - i });
g[n + i].pb({ 0,l[i] - sc + sl - i });
}
g[2 * n + 1].pb({ el,ec });
g[el].pb({ 2 * n + 1,ec });
g[2 * n + 1].pb({ el + n,l[el] - ec });
g[el + n].pb({ 2 * n + 1,l[el] - ec });
for (int i = el + 1;i <= n;i++) {
if (l[i] <= ec) {
g[2 * n + 1].pb({ i + n,i - el });
g[i + n].pb({ 2 * n + 1,i - el + ec - l[i] });
break;
}
g[2 * n + 1].pb({ n + i,l[i] - ec + i - el });
g[n + i].pb({ 2 * n + 1,l[i] - ec + i - el });
}
for (int i = el - 1;i >= 1;i--) {
if (l[i] <= ec) {
g[2 * n + 1].pb({ n + i,el - i });
g[n + i].pb({ 2 * n + 1,el - i + ec - l[i] });
break;
}
g[2 * n + 1].pb({ n + i,l[i] - ec + el - i });
g[n + i].pb({ 2 * n + 1,l[i] - ec + el - i });
}
pq.push({ 0,0 });
d[0] = 0;
while (!pq.empty()) {
int u = pq.top().second; pq.pop();
if (vis[u]) continue;
vis[u] = true;
for (auto it : g[u]) {
int v = it.first, w = it.second;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
pq.push({ -d[v], v });
}
}
}
ll mn = min(sc, ec);
for (int i = min(sl, el);i <= max(sl, el);i++) {
mn = min(l[i], mn);
}
cout << min(d[2 * n + 1],sc - mn + ec - mn + abs(sl - el)) << '\n';
}
int main() {
KarginSet();
int test = 1;
//cin >> test;
while (test--) {
KarginSolve();
}
return 0;
}
Compilation message (stderr)
Main.cpp:1:1: warning: multi-line comment [-Wcomment]
1 | //-------------dilanyan------------\\
| ^
Main.cpp:8:1: warning: multi-line comment [-Wcomment]
8 | //------------------Kargpefines--------------------\\
| ^
Main.cpp:27:1: warning: multi-line comment [-Wcomment]
27 | //-------------------KarginConstants------------------\\
| ^
Main.cpp:32:1: warning: multi-line comment [-Wcomment]
32 | //-------------------KarginCode-----------------------\\
| ^
Main.cpp: In function 'void KarginSet(std::string)':
Main.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |