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>
using namespace std;
typedef int ll; // ! CAREFUL
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef pair<int, int> pi;
typedef vector<bool> vb;
#define fi first
#define se second
#define IOS cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false)
#define pb push_back
ll solve(ll n, ll sl, ll sc, ll el, ll ec, const vll& l) {
vll prefl(n + 1, 0);
for(ll i = 0; i < n; i++) {
prefl[i + 1] = prefl[i] + l[i];
}
ll totn = prefl[n];
set<ll> vis;
typedef pair<ll, pll> ppll;
typedef vector<ppll> vppll;
// priority_queue<ppll, vppll, greater<ppll>> pq;
queue<ppll> pq;
ll s = prefl[sl] + sc, e = prefl[el] + ec;
ll ans = 0;
pq.push({0, {sl, sc}});
while(!pq.empty()) {
auto [cdist, curi] = pq.front();
pq.pop();
auto [li, ci] = curi;
ll i = prefl[li] + ci;
if(vis.count(i) > 0) continue;
vis.insert(i);
if(i == e) {
ans = cdist;
break;
}
// can move up
if(li > 0) {
pq.push({cdist + 1, {li - 1, min(ci, l[li - 1] - 1)}});
}
// can move down
if(li < n - 1) {
pq.push({cdist + 1, {li + 1, min(ci, l[li + 1] - 1)}});
}
// can move left
if(i > 0) {
if(ci > 0) {
pq.push({cdist + 1, {li, ci - 1}});
} else {
pq.push({cdist + 1, {li - 1, l[li - 1] - 1}});
}
}
// can move right
if(i < totn - 1) {
if(ci < l[li - 1] - 1) {
pq.push({cdist + 1, {li, ci + 1}});
} else {
pq.push({cdist + 1, {li + 1, 0}});
}
}
}
return ans;
}
int main() {
IOS;
ll n;
cin >> n;
ll sl, sc;
ll el, ec;
cin >> sl >> sc;
cin >> el >> ec;
sl--; sc--;
el--; ec--;
vll l(n, 0);
for(ll& lv : l) {
cin >> lv;
lv++; // ! WARNING, already added one
}
cout << solve(n, sl, sc, el, ec, l) << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'll solve(ll, ll, ll, ll, ll, const vll&)':
Main.cpp:34:8: warning: unused variable 's' [-Wunused-variable]
34 | ll s = prefl[sl] + sc, e = prefl[el] + ec;
| ^
Main.cpp:29:26: warning: typedef 'vppll' locally defined but not used [-Wunused-local-typedefs]
29 | typedef vector<ppll> vppll;
| ^~~~~
# | 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... |