#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#define MAXN 7600000
using namespace std;
int n, m, Num[MAXN], D[MAXN], Q1[MAXN], Q2[MAXN];
vector<int>E[30100];
map<int, int>Map;
bool be[MAXN], ed[MAXN];
struct A {
int a, b;
}w[30100];
int cnt;
void Make(int a, int b, int x) {
int i;
be[cnt + 1] = true;
for (i = b; i < n; i += a) {
++cnt;
Num[cnt] = i;
if (x == i) E[x].push_back(cnt);
}
ed[cnt] = true;
}
void BFS(int a) {
int i, h = 0, t = 0, tt = 0, x;
for (i = 0; i <= cnt; i++)D[i] = 999999999;
D[a] = 0;
Q1[++t] = a;
while (t) {
while (h < t) {
x = Q1[++h];
if (x < n) {
for (i = 0; i < E[x].size(); i++) {
if (D[E[x][i]] > D[x]) {
D[E[x][i]] = D[x];
Q1[++t] = E[x][i];
}
}
}
else {
if (D[Num[x]] > D[x]) {
D[Num[x]] = D[x];
Q1[++t] = Num[x];
}
}
}
tt = 0;
for (i = 1; i <= t; i++) {
if (Q1[i] < n)continue;
x = Q1[i];
if (!be[x] && D[x - 1] > D[x] + 1) {
D[x - 1] = D[x] + 1;
Q2[++tt] = x - 1;
}
if (!ed[x] && D[x + 1] > D[x] + 1) {
D[x + 1] = D[x] + 1;
Q2[++tt] = x + 1;
}
}
for (i = 1; i <= tt; i++)Q1[i] = Q2[i];
t = tt;
h = 0;
}
}
int main() {
int i, p1, p2, t;
scanf("%d%d", &n, &m);
cnt = n;
for (i = 1; i <= m; i++) {
scanf("%d%d", &w[i].a, &w[i].b);
t = w[i].b * 30100 + (w[i].a % w[i].b);
if (!Map[t]) {
Map[t] = cnt + 1;
Make(w[i].b, w[i].a % w[i].b, w[i].a);
}
else {
E[w[i].a].push_back(Map[t] + w[i].a / w[i].b);
}
}
p1 = w[1].a, p2 = w[2].a;
BFS(p1);
printf("%d\n", D[p2] == 999999999 ? -1 : D[p2]);
}
Compilation message
skyscraper.cpp: In function 'void BFS(int)':
skyscraper.cpp:34:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (i = 0; i < E[x].size(); i++) {
| ~~^~~~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d%d", &w[i].a, &w[i].b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1028 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1048 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1020 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1020 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1156 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1160 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
2 ms |
1164 KB |
Output is correct |
19 |
Correct |
1 ms |
1224 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1236 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
4 ms |
1364 KB |
Output is correct |
29 |
Correct |
2 ms |
1620 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1236 KB |
Output is correct |
33 |
Correct |
3 ms |
2388 KB |
Output is correct |
34 |
Correct |
4 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1016 KB |
Output is correct |
8 |
Correct |
1 ms |
1020 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1032 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1160 KB |
Output is correct |
19 |
Correct |
1 ms |
1160 KB |
Output is correct |
20 |
Correct |
1 ms |
1160 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1156 KB |
Output is correct |
23 |
Correct |
1 ms |
1160 KB |
Output is correct |
24 |
Correct |
2 ms |
1236 KB |
Output is correct |
25 |
Correct |
2 ms |
1288 KB |
Output is correct |
26 |
Correct |
1 ms |
1160 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
2 ms |
1620 KB |
Output is correct |
30 |
Correct |
2 ms |
1236 KB |
Output is correct |
31 |
Correct |
1 ms |
1412 KB |
Output is correct |
32 |
Correct |
2 ms |
1276 KB |
Output is correct |
33 |
Correct |
4 ms |
2264 KB |
Output is correct |
34 |
Correct |
4 ms |
2260 KB |
Output is correct |
35 |
Correct |
21 ms |
3976 KB |
Output is correct |
36 |
Correct |
4 ms |
1556 KB |
Output is correct |
37 |
Correct |
14 ms |
4240 KB |
Output is correct |
38 |
Correct |
22 ms |
4944 KB |
Output is correct |
39 |
Correct |
21 ms |
4920 KB |
Output is correct |
40 |
Correct |
23 ms |
4892 KB |
Output is correct |
41 |
Correct |
21 ms |
4908 KB |
Output is correct |
42 |
Correct |
6 ms |
1748 KB |
Output is correct |
43 |
Correct |
6 ms |
1804 KB |
Output is correct |
44 |
Correct |
9 ms |
1804 KB |
Output is correct |
45 |
Correct |
27 ms |
7932 KB |
Output is correct |
46 |
Correct |
30 ms |
7956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
1036 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1156 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
3 ms |
1240 KB |
Output is correct |
25 |
Correct |
3 ms |
1300 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
1160 KB |
Output is correct |
28 |
Correct |
3 ms |
1364 KB |
Output is correct |
29 |
Correct |
2 ms |
1664 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1280 KB |
Output is correct |
33 |
Correct |
3 ms |
2304 KB |
Output is correct |
34 |
Correct |
4 ms |
2260 KB |
Output is correct |
35 |
Correct |
22 ms |
3888 KB |
Output is correct |
36 |
Correct |
4 ms |
1492 KB |
Output is correct |
37 |
Correct |
15 ms |
4212 KB |
Output is correct |
38 |
Correct |
23 ms |
4876 KB |
Output is correct |
39 |
Correct |
21 ms |
4896 KB |
Output is correct |
40 |
Correct |
33 ms |
4940 KB |
Output is correct |
41 |
Correct |
21 ms |
4940 KB |
Output is correct |
42 |
Correct |
6 ms |
1748 KB |
Output is correct |
43 |
Correct |
9 ms |
1828 KB |
Output is correct |
44 |
Correct |
6 ms |
1760 KB |
Output is correct |
45 |
Correct |
26 ms |
7972 KB |
Output is correct |
46 |
Correct |
25 ms |
7932 KB |
Output is correct |
47 |
Correct |
31 ms |
10364 KB |
Output is correct |
48 |
Correct |
17 ms |
5196 KB |
Output is correct |
49 |
Correct |
15 ms |
4340 KB |
Output is correct |
50 |
Correct |
10 ms |
3900 KB |
Output is correct |
51 |
Correct |
30 ms |
6740 KB |
Output is correct |
52 |
Correct |
31 ms |
7048 KB |
Output is correct |
53 |
Correct |
22 ms |
4500 KB |
Output is correct |
54 |
Correct |
1 ms |
1364 KB |
Output is correct |
55 |
Correct |
1 ms |
1620 KB |
Output is correct |
56 |
Correct |
8 ms |
2700 KB |
Output is correct |
57 |
Correct |
9 ms |
5388 KB |
Output is correct |
58 |
Correct |
7 ms |
2260 KB |
Output is correct |
59 |
Correct |
7 ms |
2728 KB |
Output is correct |
60 |
Correct |
7 ms |
2952 KB |
Output is correct |
61 |
Correct |
12 ms |
3040 KB |
Output is correct |
62 |
Correct |
39 ms |
8660 KB |
Output is correct |
63 |
Correct |
113 ms |
25756 KB |
Output is correct |
64 |
Correct |
122 ms |
30668 KB |
Output is correct |
65 |
Correct |
188 ms |
42812 KB |
Output is correct |
66 |
Correct |
388 ms |
73816 KB |
Output is correct |
67 |
Correct |
342 ms |
73824 KB |
Output is correct |