// https://oj.uz/problem/view/APIO15_skyscraper
#include <bits/stdc++.h>
using namespace std;
#define Fname ((string) "io")
#define ii pair <int, int>
#define iii pair <int, ii>
#define fi first
#define se second
#define endl '\n'
const int N = 3e4 + 5;
int n, m, goal;
bitset <N> d[N], used;
vector <int> a[N];
deque <iii> dq;
int find(int u) {
dq.push_back({0, {u, 0}});
while (dq.size()) {
u = dq.front().se.fi;
int step = dq.front().se.se, total = dq.front().fi;
dq.pop_front();
if (u == goal)
return total;
if (d[u][step])
continue;
d[u][step] = 1;
if (!used[u]) {
used[u] = 1;
for (int &step : a[u])
if (!d[u][step])
dq.push_front({total, {u, step}});
}
if (u + step <= n && !d[u + step][step])
dq.push_back({total + 1, {u + step, step}});
if (u - step > 0 && !d[u - step][step])
dq.push_back({total + 1, {u - step, step}});
}
return -1;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef lan_ngu
freopen((Fname + ".inp").c_str(), "r", stdin);
freopen((Fname + ".out").c_str(), "w", stdout);
#endif
// int _nt; cin >> _nt;
int _nt = 1;
while (_nt--) {
cin >> n >> m;
int init;
for (int i = 1; i <= m; i++) {
int b, p; cin >> b >> p;
b++;
a[b].push_back(p);
if (i == 1)
init = b;
else if (i == 2)
goal = b;
}
cout << find(init);
}
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:65:20: warning: 'init' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | cout << find(init);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
968 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
968 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
2892 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
5 ms |
8396 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
4 ms |
6348 KB |
Output is correct |
24 |
Correct |
5 ms |
7500 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
4 ms |
7244 KB |
Output is correct |
27 |
Correct |
5 ms |
7628 KB |
Output is correct |
28 |
Correct |
6 ms |
8396 KB |
Output is correct |
29 |
Correct |
6 ms |
8268 KB |
Output is correct |
30 |
Correct |
6 ms |
8396 KB |
Output is correct |
31 |
Correct |
9 ms |
8396 KB |
Output is correct |
32 |
Correct |
7 ms |
8268 KB |
Output is correct |
33 |
Correct |
11 ms |
8396 KB |
Output is correct |
34 |
Correct |
9 ms |
8396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
984 KB |
Output is correct |
17 |
Correct |
2 ms |
2892 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
5 ms |
8396 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
4 ms |
6348 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
5 ms |
7680 KB |
Output is correct |
28 |
Correct |
7 ms |
8396 KB |
Output is correct |
29 |
Correct |
8 ms |
8268 KB |
Output is correct |
30 |
Correct |
6 ms |
8268 KB |
Output is correct |
31 |
Correct |
5 ms |
8268 KB |
Output is correct |
32 |
Correct |
6 ms |
8268 KB |
Output is correct |
33 |
Correct |
7 ms |
8396 KB |
Output is correct |
34 |
Correct |
6 ms |
8396 KB |
Output is correct |
35 |
Correct |
11 ms |
6588 KB |
Output is correct |
36 |
Correct |
3 ms |
2124 KB |
Output is correct |
37 |
Correct |
8 ms |
6220 KB |
Output is correct |
38 |
Correct |
10 ms |
4044 KB |
Output is correct |
39 |
Correct |
5 ms |
1228 KB |
Output is correct |
40 |
Correct |
6 ms |
2124 KB |
Output is correct |
41 |
Correct |
9 ms |
7696 KB |
Output is correct |
42 |
Correct |
8 ms |
7504 KB |
Output is correct |
43 |
Correct |
8 ms |
7884 KB |
Output is correct |
44 |
Correct |
11 ms |
8604 KB |
Output is correct |
45 |
Correct |
23 ms |
8944 KB |
Output is correct |
46 |
Correct |
13 ms |
8780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1356 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
2892 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
5 ms |
8396 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
4 ms |
6284 KB |
Output is correct |
24 |
Correct |
6 ms |
7460 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
4 ms |
7244 KB |
Output is correct |
27 |
Correct |
5 ms |
7628 KB |
Output is correct |
28 |
Correct |
6 ms |
8396 KB |
Output is correct |
29 |
Correct |
6 ms |
8292 KB |
Output is correct |
30 |
Correct |
5 ms |
8268 KB |
Output is correct |
31 |
Correct |
5 ms |
8268 KB |
Output is correct |
32 |
Correct |
5 ms |
8268 KB |
Output is correct |
33 |
Correct |
7 ms |
8396 KB |
Output is correct |
34 |
Correct |
9 ms |
8356 KB |
Output is correct |
35 |
Correct |
11 ms |
6676 KB |
Output is correct |
36 |
Correct |
2 ms |
2124 KB |
Output is correct |
37 |
Correct |
9 ms |
6256 KB |
Output is correct |
38 |
Correct |
8 ms |
4020 KB |
Output is correct |
39 |
Correct |
5 ms |
1228 KB |
Output is correct |
40 |
Correct |
10 ms |
2188 KB |
Output is correct |
41 |
Correct |
10 ms |
7772 KB |
Output is correct |
42 |
Correct |
7 ms |
7500 KB |
Output is correct |
43 |
Correct |
7 ms |
7884 KB |
Output is correct |
44 |
Correct |
9 ms |
8544 KB |
Output is correct |
45 |
Correct |
16 ms |
8924 KB |
Output is correct |
46 |
Correct |
12 ms |
8780 KB |
Output is correct |
47 |
Correct |
18 ms |
19992 KB |
Output is correct |
48 |
Correct |
6 ms |
1640 KB |
Output is correct |
49 |
Correct |
7 ms |
1684 KB |
Output is correct |
50 |
Correct |
7 ms |
1484 KB |
Output is correct |
51 |
Correct |
51 ms |
79940 KB |
Output is correct |
52 |
Correct |
60 ms |
87116 KB |
Output is correct |
53 |
Correct |
17 ms |
18708 KB |
Output is correct |
54 |
Correct |
33 ms |
61116 KB |
Output is correct |
55 |
Correct |
51 ms |
77784 KB |
Output is correct |
56 |
Correct |
68 ms |
112276 KB |
Output is correct |
57 |
Correct |
2 ms |
2636 KB |
Output is correct |
58 |
Correct |
62 ms |
111708 KB |
Output is correct |
59 |
Correct |
68 ms |
103932 KB |
Output is correct |
60 |
Correct |
68 ms |
103880 KB |
Output is correct |
61 |
Correct |
67 ms |
98696 KB |
Output is correct |
62 |
Correct |
111 ms |
112304 KB |
Output is correct |
63 |
Correct |
135 ms |
111748 KB |
Output is correct |
64 |
Correct |
166 ms |
111940 KB |
Output is correct |
65 |
Correct |
276 ms |
111864 KB |
Output is correct |
66 |
Correct |
317 ms |
111812 KB |
Output is correct |
67 |
Correct |
214 ms |
111860 KB |
Output is correct |