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 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];
vvll adj;
for(ll i = 0; i < totn; i++) {
vll adjr;
adj.pb(adjr);
}
ll s = prefl[sl] + sc, e = prefl[el] + ec;
for(ll li = 0; li < n; li++) {
for(ll ci = 0; ci < l[li]; ci++) {
ll ni = prefl[li] + ci;
ll adji;
// up
if(li > 0) {
adji = prefl[li - 1] + min(ci, l[li - 1] - 1);
adj[ni].pb(adji);
}
// down
if(li < n - 1) {
adji = prefl[li + 1] + min(ci, l[li + 1] - 1);
adj[ni].pb(adji);
}
// left
if(li > 0 || ci > 0) {
adji = ni - 1;
adj[ni].pb(adji);
}
// right
if(li < n - 1) {
adji = ni + 1;
adj[ni].pb(adji);
}
}
}
// Performing BFS
vb vis(totn, false);
queue<pll> q;
q.push({s, 0});
ll ans = 0;
while(!q.empty()) {
auto [i, dist] = q.front();
q.pop();
if(vis[i]) continue;
vis[i] = true;
if(i == e) {
ans = dist;
break;
}
for(ll j : adj[i]) {
q.push({j, dist + 1});
}
}
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;
}
# | 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... |