#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=50;
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
skyscraper.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
29 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
1484 KB |
Output is correct |
6 |
Correct |
2 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1484 KB |
Output is correct |
6 |
Correct |
2 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1484 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1612 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
4 ms |
1868 KB |
Output is correct |
15 |
Correct |
4 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1484 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1484 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1612 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
4 ms |
1740 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
5 ms |
1868 KB |
Output is correct |
15 |
Correct |
4 ms |
1740 KB |
Output is correct |
16 |
Correct |
3 ms |
2124 KB |
Output is correct |
17 |
Correct |
14 ms |
3928 KB |
Output is correct |
18 |
Correct |
22 ms |
7324 KB |
Output is correct |
19 |
Correct |
24 ms |
8076 KB |
Output is correct |
20 |
Correct |
26 ms |
8196 KB |
Output is correct |
21 |
Correct |
5 ms |
2764 KB |
Output is correct |
22 |
Correct |
20 ms |
7316 KB |
Output is correct |
23 |
Correct |
22 ms |
6828 KB |
Output is correct |
24 |
Correct |
28 ms |
7756 KB |
Output is correct |
25 |
Correct |
28 ms |
8072 KB |
Output is correct |
26 |
Correct |
30 ms |
8152 KB |
Output is correct |
27 |
Correct |
29 ms |
8296 KB |
Output is correct |
28 |
Correct |
33 ms |
8132 KB |
Output is correct |
29 |
Correct |
68 ms |
8152 KB |
Output is correct |
30 |
Correct |
34 ms |
8104 KB |
Output is correct |
31 |
Correct |
41 ms |
8156 KB |
Output is correct |
32 |
Correct |
36 ms |
8168 KB |
Output is correct |
33 |
Correct |
62 ms |
8180 KB |
Output is correct |
34 |
Correct |
63 ms |
8196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1484 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1484 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1612 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
4 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
16 |
Correct |
3 ms |
2124 KB |
Output is correct |
17 |
Correct |
14 ms |
4012 KB |
Output is correct |
18 |
Correct |
23 ms |
7340 KB |
Output is correct |
19 |
Correct |
24 ms |
8132 KB |
Output is correct |
20 |
Correct |
27 ms |
8192 KB |
Output is correct |
21 |
Correct |
5 ms |
2764 KB |
Output is correct |
22 |
Correct |
21 ms |
7404 KB |
Output is correct |
23 |
Correct |
22 ms |
6732 KB |
Output is correct |
24 |
Correct |
27 ms |
7820 KB |
Output is correct |
25 |
Correct |
28 ms |
8204 KB |
Output is correct |
26 |
Correct |
30 ms |
8128 KB |
Output is correct |
27 |
Correct |
29 ms |
8100 KB |
Output is correct |
28 |
Correct |
32 ms |
8184 KB |
Output is correct |
29 |
Correct |
53 ms |
8152 KB |
Output is correct |
30 |
Correct |
38 ms |
8140 KB |
Output is correct |
31 |
Correct |
41 ms |
8160 KB |
Output is correct |
32 |
Correct |
37 ms |
8144 KB |
Output is correct |
33 |
Correct |
69 ms |
8140 KB |
Output is correct |
34 |
Correct |
61 ms |
8164 KB |
Output is correct |
35 |
Correct |
40 ms |
6636 KB |
Output is correct |
36 |
Correct |
18 ms |
5212 KB |
Output is correct |
37 |
Correct |
63 ms |
8224 KB |
Output is correct |
38 |
Correct |
66 ms |
8520 KB |
Output is correct |
39 |
Correct |
60 ms |
8572 KB |
Output is correct |
40 |
Correct |
60 ms |
8560 KB |
Output is correct |
41 |
Correct |
60 ms |
8572 KB |
Output is correct |
42 |
Correct |
33 ms |
8388 KB |
Output is correct |
43 |
Correct |
31 ms |
8404 KB |
Output is correct |
44 |
Correct |
32 ms |
8516 KB |
Output is correct |
45 |
Correct |
75 ms |
8524 KB |
Output is correct |
46 |
Correct |
70 ms |
8492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1484 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
2 ms |
1532 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1612 KB |
Output is correct |
10 |
Correct |
3 ms |
1740 KB |
Output is correct |
11 |
Correct |
4 ms |
1868 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
5 ms |
1868 KB |
Output is correct |
16 |
Correct |
3 ms |
2124 KB |
Output is correct |
17 |
Correct |
14 ms |
3916 KB |
Output is correct |
18 |
Correct |
20 ms |
7240 KB |
Output is correct |
19 |
Correct |
26 ms |
8172 KB |
Output is correct |
20 |
Correct |
25 ms |
8132 KB |
Output is correct |
21 |
Correct |
5 ms |
2812 KB |
Output is correct |
22 |
Correct |
26 ms |
7348 KB |
Output is correct |
23 |
Correct |
29 ms |
6816 KB |
Output is correct |
24 |
Correct |
32 ms |
7756 KB |
Output is correct |
25 |
Correct |
27 ms |
8292 KB |
Output is correct |
26 |
Correct |
29 ms |
8116 KB |
Output is correct |
27 |
Correct |
28 ms |
8132 KB |
Output is correct |
28 |
Correct |
31 ms |
8140 KB |
Output is correct |
29 |
Correct |
55 ms |
8140 KB |
Output is correct |
30 |
Correct |
44 ms |
8040 KB |
Output is correct |
31 |
Correct |
41 ms |
8140 KB |
Output is correct |
32 |
Correct |
37 ms |
8096 KB |
Output is correct |
33 |
Correct |
61 ms |
8136 KB |
Output is correct |
34 |
Correct |
61 ms |
8048 KB |
Output is correct |
35 |
Correct |
39 ms |
6636 KB |
Output is correct |
36 |
Correct |
19 ms |
5212 KB |
Output is correct |
37 |
Correct |
61 ms |
8172 KB |
Output is correct |
38 |
Correct |
64 ms |
8572 KB |
Output is correct |
39 |
Correct |
61 ms |
8584 KB |
Output is correct |
40 |
Correct |
61 ms |
8476 KB |
Output is correct |
41 |
Correct |
60 ms |
8520 KB |
Output is correct |
42 |
Correct |
34 ms |
8392 KB |
Output is correct |
43 |
Correct |
42 ms |
8456 KB |
Output is correct |
44 |
Correct |
30 ms |
8468 KB |
Output is correct |
45 |
Correct |
70 ms |
8488 KB |
Output is correct |
46 |
Correct |
70 ms |
8524 KB |
Output is correct |
47 |
Correct |
552 ms |
43072 KB |
Output is correct |
48 |
Correct |
343 ms |
79304 KB |
Output is correct |
49 |
Correct |
382 ms |
86596 KB |
Output is correct |
50 |
Correct |
408 ms |
94704 KB |
Output is correct |
51 |
Correct |
699 ms |
102232 KB |
Output is correct |
52 |
Correct |
791 ms |
102212 KB |
Output is correct |
53 |
Correct |
522 ms |
102428 KB |
Output is correct |
54 |
Correct |
471 ms |
101232 KB |
Output is correct |
55 |
Correct |
462 ms |
101284 KB |
Output is correct |
56 |
Correct |
483 ms |
102668 KB |
Output is correct |
57 |
Execution timed out |
1051 ms |
101404 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |