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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=1e6+5,inf=1e18,minf=-1e18,lg=20;
int cost0[mxn+10],costlen[mxn+10],n,len[mxn+10];
pii st,ed;
int ans=inf,vis[2002][2002],len2[mxn+10];
//brute-force
int32_t main(){
cin>>n;
cin>>st.f>>st.s;
cin>>ed.f>>ed.s;
vector<int>comp;
for(int i=1;i<=n;i++){
cin>>len[i],cost0[i]=costlen[i]=inf;
len[i]++;
comp.pb(len[i]);
}
vector<vector<int>>dp(n+1,vector<int>(n+2,inf));
comp.pb(st.s);
sort(all(comp));
comp.erase(unique(all(comp)),comp.end());
priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>>pq;
pq.push({0,{st.f,lb(all(comp),st.s)-comp.begin()}});
for(int i=1;i<=n;i++)len2[i]=lb(all(comp),len[i])-comp.begin();
dp[st.f][lb(all(comp),st.s)-comp.begin()]=0;
while(!pq.empty()){
int cur=pq.top().s.f,col=pq.top().s.s,cost=pq.top().f;
pq.pop();
if(cost>dp[cur][col])continue;
if(col+1<comp.size()&&comp[col+1]<=len[cur]&&dp[cur][col+1]>dp[cur][col]+abs(comp[col+1]-comp[col])){
dp[cur][col+1]=dp[cur][col]+abs(comp[col+1]-comp[col]);
pq.push({dp[cur][col+1],{cur,col+1}});
}
if(col-1>=0&&dp[cur][col-1]>dp[cur][col]+abs(comp[col-1]-comp[col])){
dp[cur][col-1]=dp[cur][col]+abs(comp[col-1]-comp[col]);
pq.push({dp[cur][col-1],{cur,col-1}});
}
if(cur+1<=n){
int nxt=col;
if(len[cur+1]<=comp[col])nxt=len2[cur+1];
if(dp[cur+1][nxt]>dp[cur][col]+1){
dp[cur+1][nxt]=dp[cur][col]+1;
pq.push({dp[cur+1][nxt],{cur+1,nxt}});
}
if(comp[col]==len[cur]&&dp[cur+1][0]>dp[cur][col]+1){
dp[cur+1][0]=dp[cur][col]+1;
pq.push({dp[cur+1][col],{cur+1,0}});
}
}
if(cur-1>=1){
int nxt=col;
if(len[cur-1]<=comp[col])nxt=len2[cur-1];
if(dp[cur-1][nxt]>dp[cur][col]+1){
dp[cur-1][nxt]=dp[cur][col]+1;
pq.push({dp[cur-1][nxt],{cur-1,nxt}});
}
if(comp[col]==1&&dp[cur-1][len2[cur-1]]>dp[cur][col]+1){
dp[cur-1][len2[cur-1]]=dp[cur][col]+1;
pq.push({dp[cur-1][len2[cur-1]],{cur-1,len2[cur-1]}});
}
}
}
for(int i=0;i<comp.size();i++)ans=min(ans,dp[ed.f][i]+abs(comp[i]-ed.s));
cout<<ans;
}
/*
*/
Compilation message (stderr)
Main.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
Main.cpp:40:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
40 | int32_t main(){
| ^
Main.cpp: In function 'int32_t main()':
Main.cpp:62:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if(col+1<comp.size()&&comp[col+1]<=len[cur]&&dp[cur][col+1]>dp[cur][col]+abs(comp[col+1]-comp[col])){
| ~~~~~^~~~~~~~~~~~
Main.cpp:95:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=0;i<comp.size();i++)ans=min(ans,dp[ed.f][i]+abs(comp[i]-ed.s));
| ~^~~~~~~~~~~~
# | 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... |