This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#if defined(LOCAL)
#include "debug.cpp"
#else
#define debug(x...) 0
#endif // LOCAL
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
void solve(){
ll n, sr, sc, er, ec; cin >> n >> sr >> sc >> er >> ec;
vector<ll> l(n), emin(n), smin(n);
for(int i=0; i<n; i++) cin >> l[i];
sr--, sc--, er--, ec--;
emin[er]=l[er];
smin[sr]=l[sr];
for(int i=er+1; i<n; i++) emin[i]=min(emin[i-1], l[i]);
for(int i=er-1; i>=0; i--) emin[i]=min(emin[i+1], l[i]);
for(int i=sr+1; i<n; i++) smin[i]=min(smin[i-1], l[i]);
for(int i=sr-1; i>=0; i--) smin[i]=min(smin[i+1], l[i]);
ll ans = abs(er - sr) + abs(min(smin[er], sc) - ec);
for(int r=0; r<n; r++){
// mit to go to (r, 0) when not visiting (rr, 0)
// left
ll best = abs(r - sr) + min(smin[r], sc);
// right
if(r)
best = min(best, abs((r-1) - sr) + l[r-1] - min(smin[r], sc));
ll best2 = abs(r - er) + ec;
if(er+1<n)
best2=min(best2, abs(r - (er+1)) + l[er] - ec + 1);
ans = min(ans, best + best2);
}
cout<<ans<<endl;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t=1; //cin >> t;
while(t--) 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... |