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;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=50000,S=100;
int m,n,pos[N],p[N],dist[N][S];
vector<int> vec[N];
set<pair<int,pair<int,int>>> st;
main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
cin>>n>>m;
f(i,0,m){
cin>>pos[i]>>p[i];
vec[pos[i]].pb(p[i]);
}
f(i,0,n) f(j,0,S) dist[i][j]=N;
dist[pos[0]][0]=0;
f(i,0,n) f(j,0,S) st.insert({dist[i][j],{i,j}});
while(st.size()){
int x=(*st.begin()).S.F,p=(*st.begin()).S.S;
if(dist[x][p]==N) break;
//cout<<endl;
//eror(dist[2][0]);
//cout<<x<<" "<<p<<" -> "<<dist[x][p]<<endl;
st.erase(*st.begin());
if(p>0){
//eror(x+p);
//cout<<x+p<<" "<<dist[x][p]+1<<" "<<dist[x+p][0]<<endl;
if(x+p<n && dist[x][p]+1<dist[x+p][0]){
//eror(100);
st.erase({dist[x+p][0],{x+p,0}});
dist[x+p][0]=dist[x][p]+1;
st.insert({dist[x+p][0],{x+p,0}});
}
if(p<=x && dist[x][p]+1<dist[x-p][0]){
st.erase({dist[x-p][0],{x-p,0}});
dist[x-p][0]=dist[x][p]+1;
st.insert({dist[x-p][0],{x-p,0}});
}
if(x+p<n && dist[x][p]+1<dist[x+p][p]){
st.erase({dist[x+p][p],{x+p,p}});
dist[x+p][p]=dist[x][p]+1;
st.insert({dist[x+p][p],{x+p,p}});
}
if(p<=x && dist[x][p]+1<dist[x-p][p]){
st.erase({dist[x-p][p],{x-p,p}});
dist[x-p][p]=dist[x][p]+1;
st.insert({dist[x-p][p],{x-p,p}});
}
continue ;
}
for(auto u : vec[x]){
if(u<S){
if(dist[x][u]<=dist[x][p]) continue ;
st.erase({dist[x][u],{x,u}});
dist[x][u]=dist[x][p];
st.insert({dist[x][u],{x,u}});
}
else{
for(int i=x%u;i<n;i+=u){
if(dist[x][p]+abs(x-i)/u<dist[i][p]){
st.erase({dist[i][p],{i,p}});
dist[i][p]=dist[x][p]+abs(x-i)/u;
st.insert({dist[i][p],{i,p}});
}
}
}
}
}
cout<<(dist[pos[1]][0]==N ? -1 : dist[pos[1]][0]);
}
Compilation message (stderr)
skyscraper.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
29 | main(){
| ^~~~
# | 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... |