#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define sz(v) (int)v.size()
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 3e4 + 10 , maxq =2e6+8e5 , sq = 90 ,inf = 1e9 + 10 ;
int r[maxn][sq + 1] , dis[maxq] ,mark[maxn], x[maxn] , p[maxn] ;vector <int> vec[maxn];
vector <pii> G[maxq] ;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) ;
int n , m ;cin >> n >> m ;
int cnt = n+1 ;
for(int i = 0 ; i < n ; i++){
for(int j=sq;j>=1 ; j--){
G[cnt].pb({i ,0});
r[i][j] = cnt ;
if(i >= j){
G[cnt].pb({r[i-j][j] , 1});
G[r[i-j][j]].pb({cnt,1});
}
cnt++;
}
}
int x0 , x1 ;
for(int i = 0 ; i < m ; i++){
cin >> x[i]>> p[i];
if(i==0)x0 = x[i] ;
if(i == 1)x1 =x[i] ;
if(p[i] == 0)continue ;
if(p[i] > sq){
vec[x[i]].pb(p[i]);
}else{
G[x[i]].pb({r[x[i]][p[i]] , 0});
}
}
queue <int> q ;
for(int i = 0; i < cnt ; i++){dis[i] = inf ;}
dis[x0]= 0;
q.push(x0) ;
while(q.size()){
int v = q.front() ;q.pop() ;
for(pii u : G[v]){
if(dis[u.F] > dis[v]+u.S){
dis[u.F] = dis[v] + u.S ;
q.push(u.F) ;
}
}
if(v < n){
for(int z = 0 ; z < vec[v].size() ; z++){
int u = vec[v][z] ;
for(int j = v%u ; j < n ; j+=u){
if(dis[j] > dis[v]+abs(v-j)/u){
dis[j] = dis[v]+abs(v-j)/u ;
q.push(j) ;
}
}
}
}
}
if(dis[x1] == inf){
cout << "-1\n";
}else{
cout << dis[x1] << "\n";
}
}
/*
*/
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:56:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int z = 0 ; z < vec[v].size() ; z++){
| ~~^~~~~~~~~~~~~~~
skyscraper.cpp:67:11: warning: 'x1' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | if(dis[x1] == inf){
| ~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
70232 KB |
Output is correct |
2 |
Correct |
15 ms |
70236 KB |
Output is correct |
3 |
Correct |
17 ms |
70296 KB |
Output is correct |
4 |
Correct |
16 ms |
70232 KB |
Output is correct |
5 |
Correct |
16 ms |
70236 KB |
Output is correct |
6 |
Correct |
16 ms |
70236 KB |
Output is correct |
7 |
Correct |
15 ms |
70236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
70236 KB |
Output is correct |
2 |
Correct |
15 ms |
70416 KB |
Output is correct |
3 |
Correct |
17 ms |
70236 KB |
Output is correct |
4 |
Correct |
15 ms |
70232 KB |
Output is correct |
5 |
Correct |
15 ms |
70236 KB |
Output is correct |
6 |
Correct |
15 ms |
70352 KB |
Output is correct |
7 |
Correct |
15 ms |
70236 KB |
Output is correct |
8 |
Correct |
16 ms |
70492 KB |
Output is correct |
9 |
Correct |
15 ms |
70588 KB |
Output is correct |
10 |
Correct |
16 ms |
70492 KB |
Output is correct |
11 |
Correct |
16 ms |
71000 KB |
Output is correct |
12 |
Correct |
17 ms |
70744 KB |
Output is correct |
13 |
Correct |
16 ms |
70748 KB |
Output is correct |
14 |
Correct |
16 ms |
70748 KB |
Output is correct |
15 |
Correct |
16 ms |
70748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
70492 KB |
Output is correct |
2 |
Correct |
15 ms |
70412 KB |
Output is correct |
3 |
Correct |
16 ms |
70364 KB |
Output is correct |
4 |
Correct |
15 ms |
70236 KB |
Output is correct |
5 |
Correct |
17 ms |
70300 KB |
Output is correct |
6 |
Correct |
15 ms |
70236 KB |
Output is correct |
7 |
Correct |
15 ms |
70436 KB |
Output is correct |
8 |
Correct |
15 ms |
70492 KB |
Output is correct |
9 |
Correct |
15 ms |
70492 KB |
Output is correct |
10 |
Correct |
16 ms |
70744 KB |
Output is correct |
11 |
Correct |
17 ms |
70748 KB |
Output is correct |
12 |
Correct |
16 ms |
70744 KB |
Output is correct |
13 |
Correct |
16 ms |
70748 KB |
Output is correct |
14 |
Correct |
17 ms |
70748 KB |
Output is correct |
15 |
Correct |
16 ms |
70616 KB |
Output is correct |
16 |
Correct |
17 ms |
71004 KB |
Output is correct |
17 |
Correct |
23 ms |
73492 KB |
Output is correct |
18 |
Correct |
32 ms |
79708 KB |
Output is correct |
19 |
Correct |
36 ms |
80732 KB |
Output is correct |
20 |
Correct |
35 ms |
80728 KB |
Output is correct |
21 |
Correct |
20 ms |
71768 KB |
Output is correct |
22 |
Correct |
33 ms |
79700 KB |
Output is correct |
23 |
Correct |
31 ms |
78940 KB |
Output is correct |
24 |
Correct |
34 ms |
80220 KB |
Output is correct |
25 |
Correct |
35 ms |
80564 KB |
Output is correct |
26 |
Correct |
36 ms |
80732 KB |
Output is correct |
27 |
Correct |
36 ms |
80624 KB |
Output is correct |
28 |
Correct |
36 ms |
80724 KB |
Output is correct |
29 |
Correct |
37 ms |
80732 KB |
Output is correct |
30 |
Correct |
36 ms |
80756 KB |
Output is correct |
31 |
Correct |
42 ms |
80756 KB |
Output is correct |
32 |
Correct |
37 ms |
80732 KB |
Output is correct |
33 |
Correct |
39 ms |
80732 KB |
Output is correct |
34 |
Correct |
39 ms |
80732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
70260 KB |
Output is correct |
2 |
Correct |
15 ms |
70412 KB |
Output is correct |
3 |
Correct |
15 ms |
70236 KB |
Output is correct |
4 |
Correct |
15 ms |
70256 KB |
Output is correct |
5 |
Correct |
15 ms |
70236 KB |
Output is correct |
6 |
Correct |
15 ms |
70276 KB |
Output is correct |
7 |
Correct |
15 ms |
70276 KB |
Output is correct |
8 |
Correct |
16 ms |
70492 KB |
Output is correct |
9 |
Correct |
16 ms |
70492 KB |
Output is correct |
10 |
Correct |
16 ms |
70740 KB |
Output is correct |
11 |
Correct |
16 ms |
70772 KB |
Output is correct |
12 |
Correct |
16 ms |
70748 KB |
Output is correct |
13 |
Correct |
17 ms |
70748 KB |
Output is correct |
14 |
Correct |
16 ms |
70748 KB |
Output is correct |
15 |
Correct |
17 ms |
70772 KB |
Output is correct |
16 |
Correct |
18 ms |
71140 KB |
Output is correct |
17 |
Correct |
24 ms |
73396 KB |
Output is correct |
18 |
Correct |
32 ms |
79708 KB |
Output is correct |
19 |
Correct |
37 ms |
80984 KB |
Output is correct |
20 |
Correct |
35 ms |
80724 KB |
Output is correct |
21 |
Correct |
19 ms |
71772 KB |
Output is correct |
22 |
Correct |
32 ms |
79704 KB |
Output is correct |
23 |
Correct |
34 ms |
78956 KB |
Output is correct |
24 |
Correct |
35 ms |
80220 KB |
Output is correct |
25 |
Correct |
35 ms |
80724 KB |
Output is correct |
26 |
Correct |
37 ms |
80732 KB |
Output is correct |
27 |
Correct |
39 ms |
80984 KB |
Output is correct |
28 |
Correct |
36 ms |
80648 KB |
Output is correct |
29 |
Correct |
37 ms |
80724 KB |
Output is correct |
30 |
Correct |
35 ms |
80728 KB |
Output is correct |
31 |
Correct |
37 ms |
80784 KB |
Output is correct |
32 |
Correct |
35 ms |
80720 KB |
Output is correct |
33 |
Correct |
45 ms |
81036 KB |
Output is correct |
34 |
Correct |
40 ms |
80888 KB |
Output is correct |
35 |
Correct |
36 ms |
78576 KB |
Output is correct |
36 |
Correct |
28 ms |
76892 KB |
Output is correct |
37 |
Correct |
43 ms |
80720 KB |
Output is correct |
38 |
Correct |
45 ms |
80892 KB |
Output is correct |
39 |
Correct |
44 ms |
80924 KB |
Output is correct |
40 |
Correct |
45 ms |
80980 KB |
Output is correct |
41 |
Correct |
44 ms |
80988 KB |
Output is correct |
42 |
Correct |
43 ms |
81068 KB |
Output is correct |
43 |
Correct |
44 ms |
81084 KB |
Output is correct |
44 |
Correct |
44 ms |
81112 KB |
Output is correct |
45 |
Correct |
50 ms |
81264 KB |
Output is correct |
46 |
Correct |
48 ms |
80988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
70236 KB |
Output is correct |
2 |
Correct |
15 ms |
70308 KB |
Output is correct |
3 |
Correct |
16 ms |
70232 KB |
Output is correct |
4 |
Correct |
15 ms |
70232 KB |
Output is correct |
5 |
Correct |
15 ms |
70284 KB |
Output is correct |
6 |
Correct |
15 ms |
70236 KB |
Output is correct |
7 |
Correct |
15 ms |
70236 KB |
Output is correct |
8 |
Correct |
18 ms |
70492 KB |
Output is correct |
9 |
Correct |
15 ms |
70520 KB |
Output is correct |
10 |
Correct |
16 ms |
70748 KB |
Output is correct |
11 |
Correct |
16 ms |
70748 KB |
Output is correct |
12 |
Correct |
16 ms |
70768 KB |
Output is correct |
13 |
Correct |
16 ms |
71020 KB |
Output is correct |
14 |
Correct |
16 ms |
70748 KB |
Output is correct |
15 |
Correct |
16 ms |
70544 KB |
Output is correct |
16 |
Correct |
17 ms |
71044 KB |
Output is correct |
17 |
Correct |
23 ms |
73560 KB |
Output is correct |
18 |
Correct |
32 ms |
79552 KB |
Output is correct |
19 |
Correct |
36 ms |
80732 KB |
Output is correct |
20 |
Correct |
38 ms |
81244 KB |
Output is correct |
21 |
Correct |
19 ms |
71768 KB |
Output is correct |
22 |
Correct |
33 ms |
79604 KB |
Output is correct |
23 |
Correct |
31 ms |
78936 KB |
Output is correct |
24 |
Correct |
34 ms |
80220 KB |
Output is correct |
25 |
Correct |
39 ms |
80636 KB |
Output is correct |
26 |
Correct |
37 ms |
80732 KB |
Output is correct |
27 |
Correct |
37 ms |
80808 KB |
Output is correct |
28 |
Correct |
42 ms |
80968 KB |
Output is correct |
29 |
Correct |
36 ms |
80728 KB |
Output is correct |
30 |
Correct |
35 ms |
80732 KB |
Output is correct |
31 |
Correct |
37 ms |
80636 KB |
Output is correct |
32 |
Correct |
37 ms |
80724 KB |
Output is correct |
33 |
Correct |
39 ms |
80728 KB |
Output is correct |
34 |
Correct |
40 ms |
80732 KB |
Output is correct |
35 |
Correct |
36 ms |
78420 KB |
Output is correct |
36 |
Correct |
28 ms |
76896 KB |
Output is correct |
37 |
Correct |
43 ms |
80720 KB |
Output is correct |
38 |
Correct |
47 ms |
80984 KB |
Output is correct |
39 |
Correct |
44 ms |
80984 KB |
Output is correct |
40 |
Correct |
46 ms |
80904 KB |
Output is correct |
41 |
Correct |
45 ms |
80968 KB |
Output is correct |
42 |
Correct |
38 ms |
81108 KB |
Output is correct |
43 |
Correct |
38 ms |
81128 KB |
Output is correct |
44 |
Correct |
37 ms |
81144 KB |
Output is correct |
45 |
Correct |
47 ms |
80988 KB |
Output is correct |
46 |
Correct |
48 ms |
80984 KB |
Output is correct |
47 |
Correct |
201 ms |
131196 KB |
Output is correct |
48 |
Correct |
240 ms |
185172 KB |
Output is correct |
49 |
Correct |
253 ms |
196436 KB |
Output is correct |
50 |
Correct |
276 ms |
207488 KB |
Output is correct |
51 |
Correct |
331 ms |
216148 KB |
Output is correct |
52 |
Correct |
352 ms |
216316 KB |
Output is correct |
53 |
Correct |
328 ms |
216148 KB |
Output is correct |
54 |
Correct |
297 ms |
215572 KB |
Output is correct |
55 |
Correct |
314 ms |
215580 KB |
Output is correct |
56 |
Correct |
302 ms |
216420 KB |
Output is correct |
57 |
Correct |
358 ms |
215620 KB |
Output is correct |
58 |
Correct |
304 ms |
215840 KB |
Output is correct |
59 |
Correct |
319 ms |
215832 KB |
Output is correct |
60 |
Correct |
318 ms |
215896 KB |
Output is correct |
61 |
Correct |
376 ms |
215804 KB |
Output is correct |
62 |
Correct |
354 ms |
216516 KB |
Output is correct |
63 |
Correct |
368 ms |
216140 KB |
Output is correct |
64 |
Correct |
363 ms |
215928 KB |
Output is correct |
65 |
Correct |
381 ms |
216188 KB |
Output is correct |
66 |
Correct |
531 ms |
216176 KB |
Output is correct |
67 |
Correct |
547 ms |
216180 KB |
Output is correct |