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(){
int n, sr, sc, er, ec; cin >> n >> sr >> sc >> er >> ec;
vector<ll> l(n);
for(int i=0; i<n; i++) cin >> l[i];
sr--, sc--, er--, ec--;
vector<map<int, ll>> row(n);
// (cost, r, c)
priority_queue<array<ll, 3>, vector<array<ll, 3>>, greater<array<ll, 3>>> pq;
pq.push({0, sr, sc});
while(pq.size()){
auto [cost, r, c] = pq.top();
pq.pop();
if(row[r].count(c)){
continue;
}
row[r][c]=cost;
// if begin
if(c==0 and r!=0){
pq.push({cost + 1, r-1, l[r-1]});
}
// if end
if(c==l[r] and r!=n-1){
pq.push({cost + 1, r + 1, 0});
}
// mid
// go left, right, up, down
pq.push({cost + c, r, 0});
pq.push({cost + l[r] - c, r, l[r]});
pq.push({cost + l[r] - c, r, l[r]});
if(r!=0){
pq.push({cost + 1, r-1, min(l[r-1], c)});
}if(r!=n-1){
pq.push({cost + 1, r+1, min(l[r+1], c)});
}
}
ll ans = 1e15;
for(auto [c, cost]:row[er]){
ans = min(ans, abs(ec - c) + cost);
}
cout << ans << endl;
for(auto r:row){
debug(r);
}
}
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:58:3: note: in expansion of macro 'debug'
58 | debug(r);
| ^~~~~
# | 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... |