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() {
int n, sl, sc, el, ec;
cin >> n >> sl >> sc >> el >> ec;
sc--, ec--;
for (int i = 1;i <= n;i++) cin >> l[i];
if (ec == 0) {
cout << sc + abs(sl - el);
return;
}
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 + 1, inf);
vector<bool> vis(2 * n + 1, false);
pq.push({ -sc,sl });
d[sl] = sc;
for (int i = sl;i <= n;i++) {
if (l[i] < sc) {
d[n + i] = i - sl;
pq.push({ -d[n + i],n + i });
break;
}
pq.push({ -(l[i] - sc + i - sl), n + i});
d[n + i] = l[i] - sc + i - sl;
}
for (int i = sl - 1;i >= 1;i--) {
if (l[i] < sc) {
d[n + i] = sl - i;
pq.push({ -d[n + i],n + i });
break;
}
pq.push({ -(l[i] - sc + sl - i), n + i });
d[n + i] = l[i] - sc + sl - i;
}
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 ans = inf;
for (int i = 1;i <= n;i++) {
ans = min(ans, d[i] + ec + abs(i - el));
}
ll mn = ec;
for (int i = el - 1;i >= 1;i--) {
mn = min(mn, l[i]);
if (mn == l[i]) {
ans = min(ans, el - i + d[n + i] + min(ec - l[i],l[i] + l[el] - ec + 2));
}
}
mn = ec;
for (int i = el + 1;i <= n;i++) {
mn = min(mn, l[i]);
if (mn == l[i]) {
ans = min(ans, i - el + d[n + i] + min(ec - l[i], l[i] + l[el] - ec));
}
}
cout << ans << '\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... |