#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int N, M, b[30005], p[30005], seen[30005];
unordered_set<int> doges[30005];
unordered_map<int, int> dists;
queue<int> q;
int id(int d, int x) {
return d * 30001 + x;
}
void visit(int pos, int d) {
seen[pos] = 1;
for (int doge : doges[pos]) {
if (dists.find(id(pos, doge)) == dists.end()) {
dists[id(pos, doge)] = d;
q.push(id(pos, doge));
}
}
}
int main() {
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
scanf("%d %d", b+i, p+i);
doges[b[i]].insert(p[i]);
}
visit(b[0], 0);
while (q.size()) {
int e = q.front(); q.pop();
int loc = e / 30001, power = e % 30001;
if (loc == b[1]) {
printf("%d\n", dists[e]);
return 0;
}
if (loc - power >= 0) {
int pos = loc - power;
int d = power;
if (!seen[pos]) {
visit(pos, dists[e] + 1);
}
if (dists.find(id(pos, d)) == dists.end()) {
dists[id(pos, d)] = dists[e] + 1;
q.push(id(pos, d));
}
}
if (loc + power < N) {
int pos = loc + power;
int d = power;
if (!seen[pos]) {
visit(pos, dists[e] + 1);
}
if (dists.find(id(pos, d)) == dists.end()) {
dists[id(pos, d)] = dists[e] + 1;
q.push(id(pos, d));
}
}
}
printf("-1\n");
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", b+i, p+i);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
5 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
6 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
5 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
1920 KB |
Output is correct |
5 |
Correct |
5 ms |
2048 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
2048 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
6 ms |
2048 KB |
Output is correct |
11 |
Correct |
6 ms |
2176 KB |
Output is correct |
12 |
Correct |
6 ms |
2048 KB |
Output is correct |
13 |
Correct |
6 ms |
2048 KB |
Output is correct |
14 |
Correct |
7 ms |
2304 KB |
Output is correct |
15 |
Correct |
8 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
5 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
2048 KB |
Output is correct |
5 |
Correct |
5 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
7 ms |
2048 KB |
Output is correct |
11 |
Correct |
6 ms |
2176 KB |
Output is correct |
12 |
Correct |
6 ms |
2048 KB |
Output is correct |
13 |
Correct |
6 ms |
2048 KB |
Output is correct |
14 |
Correct |
7 ms |
2304 KB |
Output is correct |
15 |
Correct |
7 ms |
2304 KB |
Output is correct |
16 |
Correct |
6 ms |
2048 KB |
Output is correct |
17 |
Correct |
7 ms |
2304 KB |
Output is correct |
18 |
Correct |
6 ms |
2048 KB |
Output is correct |
19 |
Correct |
6 ms |
2048 KB |
Output is correct |
20 |
Correct |
6 ms |
2176 KB |
Output is correct |
21 |
Correct |
6 ms |
2048 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
6 ms |
2176 KB |
Output is correct |
24 |
Correct |
7 ms |
2304 KB |
Output is correct |
25 |
Correct |
7 ms |
2176 KB |
Output is correct |
26 |
Correct |
6 ms |
2176 KB |
Output is correct |
27 |
Correct |
7 ms |
2176 KB |
Output is correct |
28 |
Correct |
10 ms |
2944 KB |
Output is correct |
29 |
Correct |
16 ms |
4912 KB |
Output is correct |
30 |
Correct |
8 ms |
2816 KB |
Output is correct |
31 |
Correct |
10 ms |
3504 KB |
Output is correct |
32 |
Correct |
9 ms |
2944 KB |
Output is correct |
33 |
Correct |
28 ms |
8080 KB |
Output is correct |
34 |
Correct |
18 ms |
5040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1920 KB |
Output is correct |
2 |
Correct |
5 ms |
1920 KB |
Output is correct |
3 |
Correct |
5 ms |
1920 KB |
Output is correct |
4 |
Correct |
5 ms |
2048 KB |
Output is correct |
5 |
Correct |
5 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
6 ms |
1920 KB |
Output is correct |
10 |
Correct |
6 ms |
2048 KB |
Output is correct |
11 |
Correct |
6 ms |
2176 KB |
Output is correct |
12 |
Correct |
6 ms |
2048 KB |
Output is correct |
13 |
Correct |
6 ms |
2048 KB |
Output is correct |
14 |
Correct |
7 ms |
2304 KB |
Output is correct |
15 |
Correct |
7 ms |
2304 KB |
Output is correct |
16 |
Correct |
6 ms |
2048 KB |
Output is correct |
17 |
Correct |
7 ms |
2304 KB |
Output is correct |
18 |
Correct |
6 ms |
2048 KB |
Output is correct |
19 |
Correct |
6 ms |
2048 KB |
Output is correct |
20 |
Correct |
7 ms |
2176 KB |
Output is correct |
21 |
Correct |
6 ms |
2048 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
7 ms |
2304 KB |
Output is correct |
24 |
Correct |
8 ms |
2432 KB |
Output is correct |
25 |
Correct |
6 ms |
2176 KB |
Output is correct |
26 |
Correct |
6 ms |
2176 KB |
Output is correct |
27 |
Correct |
7 ms |
2176 KB |
Output is correct |
28 |
Correct |
9 ms |
2944 KB |
Output is correct |
29 |
Correct |
16 ms |
5040 KB |
Output is correct |
30 |
Correct |
8 ms |
2688 KB |
Output is correct |
31 |
Correct |
10 ms |
3504 KB |
Output is correct |
32 |
Correct |
9 ms |
2944 KB |
Output is correct |
33 |
Correct |
28 ms |
8072 KB |
Output is correct |
34 |
Correct |
19 ms |
5040 KB |
Output is correct |
35 |
Correct |
25 ms |
5424 KB |
Output is correct |
36 |
Correct |
10 ms |
2304 KB |
Output is correct |
37 |
Correct |
21 ms |
5040 KB |
Output is correct |
38 |
Correct |
29 ms |
6824 KB |
Output is correct |
39 |
Correct |
18 ms |
3840 KB |
Output is correct |
40 |
Correct |
22 ms |
4864 KB |
Output is correct |
41 |
Correct |
30 ms |
5808 KB |
Output is correct |
42 |
Correct |
12 ms |
2560 KB |
Output is correct |
43 |
Correct |
13 ms |
2560 KB |
Output is correct |
44 |
Correct |
12 ms |
2560 KB |
Output is correct |
45 |
Correct |
200 ms |
28296 KB |
Output is correct |
46 |
Correct |
80 ms |
15932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1920 KB |
Output is correct |
2 |
Correct |
6 ms |
2048 KB |
Output is correct |
3 |
Correct |
6 ms |
2048 KB |
Output is correct |
4 |
Correct |
6 ms |
1920 KB |
Output is correct |
5 |
Correct |
5 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
5 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
2048 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
6 ms |
2048 KB |
Output is correct |
11 |
Correct |
6 ms |
2176 KB |
Output is correct |
12 |
Correct |
6 ms |
2048 KB |
Output is correct |
13 |
Correct |
6 ms |
2048 KB |
Output is correct |
14 |
Correct |
8 ms |
2304 KB |
Output is correct |
15 |
Correct |
7 ms |
2304 KB |
Output is correct |
16 |
Correct |
6 ms |
2048 KB |
Output is correct |
17 |
Correct |
7 ms |
2304 KB |
Output is correct |
18 |
Correct |
6 ms |
2048 KB |
Output is correct |
19 |
Correct |
6 ms |
2048 KB |
Output is correct |
20 |
Correct |
6 ms |
2176 KB |
Output is correct |
21 |
Correct |
6 ms |
2048 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
6 ms |
2304 KB |
Output is correct |
24 |
Correct |
7 ms |
2384 KB |
Output is correct |
25 |
Correct |
6 ms |
2176 KB |
Output is correct |
26 |
Correct |
6 ms |
2176 KB |
Output is correct |
27 |
Correct |
6 ms |
2176 KB |
Output is correct |
28 |
Correct |
10 ms |
2944 KB |
Output is correct |
29 |
Correct |
16 ms |
5040 KB |
Output is correct |
30 |
Correct |
8 ms |
2688 KB |
Output is correct |
31 |
Correct |
14 ms |
3504 KB |
Output is correct |
32 |
Correct |
9 ms |
2944 KB |
Output is correct |
33 |
Correct |
28 ms |
8080 KB |
Output is correct |
34 |
Correct |
17 ms |
5040 KB |
Output is correct |
35 |
Correct |
25 ms |
5424 KB |
Output is correct |
36 |
Correct |
8 ms |
2304 KB |
Output is correct |
37 |
Correct |
22 ms |
5040 KB |
Output is correct |
38 |
Correct |
38 ms |
6952 KB |
Output is correct |
39 |
Correct |
19 ms |
3840 KB |
Output is correct |
40 |
Correct |
23 ms |
4864 KB |
Output is correct |
41 |
Correct |
27 ms |
5800 KB |
Output is correct |
42 |
Correct |
11 ms |
2560 KB |
Output is correct |
43 |
Correct |
11 ms |
2560 KB |
Output is correct |
44 |
Correct |
12 ms |
2560 KB |
Output is correct |
45 |
Correct |
196 ms |
28292 KB |
Output is correct |
46 |
Correct |
77 ms |
15928 KB |
Output is correct |
47 |
Correct |
27 ms |
5552 KB |
Output is correct |
48 |
Correct |
16 ms |
3584 KB |
Output is correct |
49 |
Correct |
15 ms |
3456 KB |
Output is correct |
50 |
Correct |
13 ms |
3072 KB |
Output is correct |
51 |
Correct |
34 ms |
6312 KB |
Output is correct |
52 |
Correct |
36 ms |
7212 KB |
Output is correct |
53 |
Correct |
26 ms |
4496 KB |
Output is correct |
54 |
Correct |
7 ms |
2816 KB |
Output is correct |
55 |
Correct |
9 ms |
3072 KB |
Output is correct |
56 |
Correct |
23 ms |
5888 KB |
Output is correct |
57 |
Correct |
7 ms |
2176 KB |
Output is correct |
58 |
Correct |
23 ms |
5392 KB |
Output is correct |
59 |
Correct |
22 ms |
5552 KB |
Output is correct |
60 |
Correct |
21 ms |
5424 KB |
Output is correct |
61 |
Correct |
23 ms |
5680 KB |
Output is correct |
62 |
Correct |
201 ms |
29060 KB |
Output is correct |
63 |
Execution timed out |
1097 ms |
70608 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |