이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
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;
ans = min(ans, abs(sr - r) + abs(r - er) + abs(ec - min({smin[r], emin[r], sc})));
}
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();
}
# | 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... |