#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N = 3e4+5, mod = 1e9+7, S = 500;
int n,m;
int sq,pos,power;
int cnt,st,f;
int D[N],fix[N];
vector<pair<int,int>>gr[N];
vector<int>on_pos[N];
bool is[N][S];
int Pos[N];
inline void test_case(){
cin >> n >> m;
sq = sqrt(n);
cnt = n;
for(int i = 1; i <= m; i++){
cin >> pos >> power;
pos++;
on_pos[pos].pb(power);
if(power <= sq) is[pos][power] = 1;
Pos[i] = pos;
}
for(int i = 1; i <= cnt; i++) D[i] = 1e9;
priority_queue<pair<int,int>>q;
q.push({0,Pos[1]});
D[Pos[1]] = 0;
while(q.size()){
auto p = q.top();
q.pop();
int x = p.ss;
int dist = -p.ff;
if(fix[x] == 1) continue;
fix[x] = 1;
if(x == Pos[2]){
cout << dist << endl;
return ;
}
sort(on_pos[x].begin(),on_pos[x].end());
int last = 0;
for(auto power : on_pos[x]){
if(power == last) continue;
last = power;
for(int to = x+power; to <= n; to += power){
int new_dist = dist + (to-x)/power;
if(new_dist < D[to]){
D[to] = new_dist;
q.push({-D[to],to});
}
if(power <= sq && is[to][power]) break;
}
for(int to = x-power; to >= 1; to -= power){
int new_dist = dist + abs(to-x)/power;
if(new_dist < D[to]){
D[to] = new_dist;
q.push({-D[to],to});
}
if(power <= sq && is[to][power]) break;
}
}
}
if(D[Pos[2]] == 1e9) D[Pos[2]] = -1;
cout << D[Pos[2]] << endl;
}
main(){
ios::sync_with_stdio(0);
int T = 1;
//cin >> T;
while(T--){
test_case();
}
}
Compilation message
skyscraper.cpp:78:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
78 | main(){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1740 KB |
Output is correct |
2 |
Correct |
1 ms |
1740 KB |
Output is correct |
3 |
Correct |
2 ms |
1740 KB |
Output is correct |
4 |
Correct |
1 ms |
1740 KB |
Output is correct |
5 |
Correct |
1 ms |
1740 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1740 KB |
Output is correct |
2 |
Correct |
1 ms |
1740 KB |
Output is correct |
3 |
Correct |
1 ms |
1740 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1740 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
2 ms |
1740 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
1 ms |
1740 KB |
Output is correct |
10 |
Correct |
1 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 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 |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1740 KB |
Output is correct |
2 |
Correct |
1 ms |
1740 KB |
Output is correct |
3 |
Correct |
1 ms |
1740 KB |
Output is correct |
4 |
Correct |
1 ms |
1740 KB |
Output is correct |
5 |
Correct |
1 ms |
1740 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1740 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
1 ms |
1740 KB |
Output is correct |
10 |
Correct |
1 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 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 |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
1 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1996 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
2 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
2 ms |
1868 KB |
Output is correct |
25 |
Correct |
2 ms |
1740 KB |
Output is correct |
26 |
Correct |
2 ms |
1868 KB |
Output is correct |
27 |
Correct |
2 ms |
1812 KB |
Output is correct |
28 |
Correct |
2 ms |
1868 KB |
Output is correct |
29 |
Correct |
3 ms |
1868 KB |
Output is correct |
30 |
Correct |
2 ms |
1868 KB |
Output is correct |
31 |
Correct |
2 ms |
1868 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
4 ms |
1996 KB |
Output is correct |
34 |
Correct |
3 ms |
2012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1740 KB |
Output is correct |
2 |
Correct |
1 ms |
1740 KB |
Output is correct |
3 |
Correct |
1 ms |
1740 KB |
Output is correct |
4 |
Correct |
1 ms |
1740 KB |
Output is correct |
5 |
Correct |
1 ms |
1740 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1740 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
1 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 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 |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1996 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
2 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
2 ms |
1868 KB |
Output is correct |
25 |
Correct |
2 ms |
1740 KB |
Output is correct |
26 |
Correct |
2 ms |
1868 KB |
Output is correct |
27 |
Correct |
2 ms |
1740 KB |
Output is correct |
28 |
Correct |
2 ms |
1868 KB |
Output is correct |
29 |
Correct |
3 ms |
1868 KB |
Output is correct |
30 |
Correct |
2 ms |
1868 KB |
Output is correct |
31 |
Correct |
3 ms |
1868 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
4 ms |
1996 KB |
Output is correct |
34 |
Correct |
4 ms |
2124 KB |
Output is correct |
35 |
Correct |
8 ms |
2764 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
9 ms |
3020 KB |
Output is correct |
38 |
Correct |
10 ms |
3020 KB |
Output is correct |
39 |
Correct |
8 ms |
3020 KB |
Output is correct |
40 |
Correct |
9 ms |
3020 KB |
Output is correct |
41 |
Correct |
10 ms |
2944 KB |
Output is correct |
42 |
Correct |
6 ms |
2124 KB |
Output is correct |
43 |
Correct |
7 ms |
2124 KB |
Output is correct |
44 |
Correct |
7 ms |
3092 KB |
Output is correct |
45 |
Correct |
12 ms |
2252 KB |
Output is correct |
46 |
Correct |
11 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1740 KB |
Output is correct |
2 |
Correct |
1 ms |
1740 KB |
Output is correct |
3 |
Correct |
1 ms |
1740 KB |
Output is correct |
4 |
Correct |
1 ms |
1740 KB |
Output is correct |
5 |
Correct |
1 ms |
1740 KB |
Output is correct |
6 |
Correct |
1 ms |
1740 KB |
Output is correct |
7 |
Correct |
1 ms |
1740 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
1 ms |
1740 KB |
Output is correct |
10 |
Correct |
1 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 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 |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1996 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
2 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
3 ms |
1868 KB |
Output is correct |
25 |
Correct |
2 ms |
1740 KB |
Output is correct |
26 |
Correct |
3 ms |
1868 KB |
Output is correct |
27 |
Correct |
2 ms |
1740 KB |
Output is correct |
28 |
Correct |
2 ms |
1868 KB |
Output is correct |
29 |
Correct |
3 ms |
1868 KB |
Output is correct |
30 |
Correct |
3 ms |
1868 KB |
Output is correct |
31 |
Correct |
2 ms |
1868 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
4 ms |
1996 KB |
Output is correct |
34 |
Correct |
3 ms |
1996 KB |
Output is correct |
35 |
Correct |
8 ms |
2764 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
7 ms |
2948 KB |
Output is correct |
38 |
Correct |
13 ms |
3040 KB |
Output is correct |
39 |
Correct |
9 ms |
2976 KB |
Output is correct |
40 |
Correct |
9 ms |
3108 KB |
Output is correct |
41 |
Correct |
10 ms |
3020 KB |
Output is correct |
42 |
Correct |
6 ms |
2124 KB |
Output is correct |
43 |
Correct |
6 ms |
2124 KB |
Output is correct |
44 |
Correct |
7 ms |
3020 KB |
Output is correct |
45 |
Correct |
12 ms |
2252 KB |
Output is correct |
46 |
Correct |
11 ms |
2340 KB |
Output is correct |
47 |
Correct |
12 ms |
5880 KB |
Output is correct |
48 |
Correct |
8 ms |
2896 KB |
Output is correct |
49 |
Correct |
7 ms |
2764 KB |
Output is correct |
50 |
Correct |
6 ms |
2636 KB |
Output is correct |
51 |
Correct |
20 ms |
3912 KB |
Output is correct |
52 |
Correct |
24 ms |
4088 KB |
Output is correct |
53 |
Correct |
13 ms |
2804 KB |
Output is correct |
54 |
Correct |
5 ms |
2124 KB |
Output is correct |
55 |
Correct |
6 ms |
2244 KB |
Output is correct |
56 |
Correct |
21 ms |
17612 KB |
Output is correct |
57 |
Correct |
7 ms |
9164 KB |
Output is correct |
58 |
Correct |
11 ms |
2468 KB |
Output is correct |
59 |
Correct |
11 ms |
2764 KB |
Output is correct |
60 |
Correct |
11 ms |
3060 KB |
Output is correct |
61 |
Correct |
12 ms |
2892 KB |
Output is correct |
62 |
Correct |
17 ms |
3416 KB |
Output is correct |
63 |
Correct |
39 ms |
3604 KB |
Output is correct |
64 |
Correct |
40 ms |
3556 KB |
Output is correct |
65 |
Correct |
52 ms |
3688 KB |
Output is correct |
66 |
Correct |
98 ms |
4672 KB |
Output is correct |
67 |
Correct |
76 ms |
4620 KB |
Output is correct |