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), t(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);
debug(ans);
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-1], sc) + 1);
t[r] = best;
}
for(int r=1; r<n; r++){
t[r]=min(t[r], t[r-1]+1);
}
for(int r=n-2; r>=0; r--){
t[r]=min(t[r], t[r+1]+1);
}
for(int r=0; r<n; r++){
ll best2 = abs(r - er) + ec;
if(r)
best2=min(best2, abs((r-1) - er) + abs(min(emin[r-1], l[r-1]) - ec) + 1);
ans = min(ans, t[r] + best2);
}
cout<<ans<<endl;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t=1; //cin >> t;
while(t--) solve();
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:6:21: warning: statement has no effect [-Wunused-value]
6 | #define debug(x...) 0
| ^
Main.cpp:29:2: note: in expansion of macro 'debug'
29 | debug(ans);
| ^~~~~
# | 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... |