#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
map<int,int> d[maxn];
int n,l[maxn];
pii s,t;
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
cin >> s.fi >> s.se;
cin >> t.fi >> t.se;
fo(i,1,n) cin >> l[i];
priority_queue<array<int,3>,vector<array<int,3>>,greater<array<int,3>>> q;
q.push({0,s.fi,s.se});
while(ss(q))
{
auto [du,x,y] = q.top();
q.pop();
if(du != d[x][y]) continue;
// up
if(x != 1)
{
if(!d[x - 1].count(min(y,l[x - 1] + 1)) or d[x - 1][min(y,l[x - 1] + 1)] > du + 1)
{
d[x - 1][min(y,l[x - 1] + 1)] = du + 1;
q.push({du + 1,x - 1,min(l[x - 1] + 1,y)});
}
}
//down
if(x != n)
{
if(!d[x + 1].count(min(y,l[x + 1] + 1)) or d[x + 1][min(y,l[x + 1] + 1)] > du + 1)
{
d[x + 1][min(y,l[x + 1] + 1)] = du + 1;
q.push({du + 1,x + 1,min(l[x + 1] + 1,y)});
}
}
// begin
if(x != 1 and y == 1)
{
if(!d[x - 1].count(l[x - 1] + 1) or d[x - 1][l[x - 1] + 1] > du + 1)
{
d[x - 1][l[x - 1] + 1] = du + 1;
q.push({du + 1,x - 1,l[x - 1] + 1});
}
}
// end
if(x != n and y == l[x] + 1)
{
if(!d[x + 1].count(1) or d[x + 1][1] > du + 1)
{
d[x + 1][1] = du + 1;
q.push({du + 1,x + 1,1});
}
}
//go to begin
if(!d[x].count(1) or d[x][1] > du + y - 1)
{
d[x][1] = du + y - 1;
q.push({du + y - 1,x,1});
}
if(!d[x].count(l[x] + 1) or d[x][l[x] + 1] > du + l[x] + 1 - y)
{
d[x][l[x] + 1] = du + l[x] + 1 - y;
q.push({du + l[x] + 1 - y,x,l[x] + 1});
}
}
int ans = inf;
for(auto it : d[t.fi]) minimize(ans,it.se + abs(t.se - it.fi));
cout << ans;
}
Compilation message (stderr)
Main.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |