#include <iostream>
#include <vector>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,fma")
using namespace std;
vector<int> g[30005];
int f[30005], da[30005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m, h, k;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
if (i == 1) h = x;
if (i == 2) k = x;
g[x].push_back(y);
}
for (int i = 0; i < n; i++) f[i] = 1e9;
f[h] = 0;
for (int jj = 0; jj < n; jj++) {
int p, mi = 1e9;
for (int i = 0; i < n; i++) {
if (da[i] == 0 && mi > f[i]) {
mi = f[i];
p = i;
}
}
if (p == k) break;
da[p] = 1;
for (int w : g[p]) {
int l = 0;
for (int j = p - w; j >= 0; j -= w) {
f[j] = min(f[j], f[p] + l + 1);
l++;
}
l = 0;
for (int j = p + w; j < n; j += w) {
f[j] = min(f[j], f[p] + l + 1);
l++;
}
}
}
if (f[k] == 1e9) cout << -1;
else cout << f[k];
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:40:37: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
40 | f[j] = min(f[j], f[p] + l + 1);
| ~~~^
skyscraper.cpp:30:9: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | if (p == k) break;
| ^~
skyscraper.cpp:21:10: warning: 'h' may be used uninitialized in this function [-Wmaybe-uninitialized]
21 | f[h] = 0;
| ~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1104 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
2 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1104 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
1268 KB |
Output is correct |
5 |
Correct |
1 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
1104 KB |
Output is correct |
9 |
Correct |
1 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
1 ms |
1104 KB |
Output is correct |
12 |
Correct |
1 ms |
1104 KB |
Output is correct |
13 |
Correct |
1 ms |
1104 KB |
Output is correct |
14 |
Correct |
1 ms |
1104 KB |
Output is correct |
15 |
Correct |
2 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1272 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
1104 KB |
Output is correct |
5 |
Correct |
1 ms |
1104 KB |
Output is correct |
6 |
Correct |
2 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
1104 KB |
Output is correct |
9 |
Correct |
1 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
2 ms |
1268 KB |
Output is correct |
12 |
Correct |
2 ms |
1104 KB |
Output is correct |
13 |
Correct |
2 ms |
1104 KB |
Output is correct |
14 |
Correct |
2 ms |
1104 KB |
Output is correct |
15 |
Correct |
1 ms |
1104 KB |
Output is correct |
16 |
Correct |
1 ms |
1104 KB |
Output is correct |
17 |
Correct |
2 ms |
1104 KB |
Output is correct |
18 |
Correct |
3 ms |
1216 KB |
Output is correct |
19 |
Correct |
4 ms |
1104 KB |
Output is correct |
20 |
Correct |
8 ms |
1104 KB |
Output is correct |
21 |
Correct |
2 ms |
1104 KB |
Output is correct |
22 |
Correct |
3 ms |
1208 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
4 ms |
1104 KB |
Output is correct |
25 |
Correct |
2 ms |
1104 KB |
Output is correct |
26 |
Correct |
4 ms |
1104 KB |
Output is correct |
27 |
Correct |
4 ms |
1104 KB |
Output is correct |
28 |
Correct |
4 ms |
1104 KB |
Output is correct |
29 |
Correct |
4 ms |
1104 KB |
Output is correct |
30 |
Correct |
3 ms |
1104 KB |
Output is correct |
31 |
Correct |
4 ms |
1104 KB |
Output is correct |
32 |
Correct |
4 ms |
1104 KB |
Output is correct |
33 |
Correct |
4 ms |
1104 KB |
Output is correct |
34 |
Correct |
4 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1104 KB |
Output is correct |
2 |
Correct |
2 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1272 KB |
Output is correct |
4 |
Correct |
2 ms |
1104 KB |
Output is correct |
5 |
Correct |
1 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1272 KB |
Output is correct |
8 |
Correct |
1 ms |
1104 KB |
Output is correct |
9 |
Correct |
1 ms |
1104 KB |
Output is correct |
10 |
Correct |
2 ms |
1104 KB |
Output is correct |
11 |
Correct |
2 ms |
1104 KB |
Output is correct |
12 |
Correct |
2 ms |
1104 KB |
Output is correct |
13 |
Correct |
2 ms |
1104 KB |
Output is correct |
14 |
Correct |
2 ms |
1104 KB |
Output is correct |
15 |
Correct |
2 ms |
1104 KB |
Output is correct |
16 |
Correct |
1 ms |
1104 KB |
Output is correct |
17 |
Correct |
2 ms |
1104 KB |
Output is correct |
18 |
Correct |
3 ms |
1104 KB |
Output is correct |
19 |
Correct |
4 ms |
1104 KB |
Output is correct |
20 |
Correct |
5 ms |
1104 KB |
Output is correct |
21 |
Correct |
2 ms |
1104 KB |
Output is correct |
22 |
Correct |
3 ms |
1104 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
4 ms |
1104 KB |
Output is correct |
25 |
Correct |
2 ms |
1104 KB |
Output is correct |
26 |
Correct |
4 ms |
1104 KB |
Output is correct |
27 |
Correct |
3 ms |
1104 KB |
Output is correct |
28 |
Correct |
5 ms |
1328 KB |
Output is correct |
29 |
Correct |
4 ms |
1104 KB |
Output is correct |
30 |
Correct |
4 ms |
1104 KB |
Output is correct |
31 |
Correct |
4 ms |
1104 KB |
Output is correct |
32 |
Correct |
4 ms |
1272 KB |
Output is correct |
33 |
Correct |
4 ms |
1104 KB |
Output is correct |
34 |
Correct |
3 ms |
1104 KB |
Output is correct |
35 |
Correct |
6 ms |
1336 KB |
Output is correct |
36 |
Correct |
2 ms |
1104 KB |
Output is correct |
37 |
Correct |
6 ms |
1360 KB |
Output is correct |
38 |
Correct |
6 ms |
1360 KB |
Output is correct |
39 |
Correct |
5 ms |
1532 KB |
Output is correct |
40 |
Correct |
5 ms |
1360 KB |
Output is correct |
41 |
Correct |
5 ms |
1360 KB |
Output is correct |
42 |
Correct |
6 ms |
1360 KB |
Output is correct |
43 |
Correct |
6 ms |
1360 KB |
Output is correct |
44 |
Correct |
45 ms |
1360 KB |
Output is correct |
45 |
Correct |
8 ms |
1532 KB |
Output is correct |
46 |
Correct |
7 ms |
1360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1104 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1104 KB |
Output is correct |
4 |
Correct |
2 ms |
1104 KB |
Output is correct |
5 |
Correct |
1 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
1104 KB |
Output is correct |
9 |
Correct |
1 ms |
1104 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
2 ms |
1104 KB |
Output is correct |
12 |
Correct |
2 ms |
1104 KB |
Output is correct |
13 |
Correct |
2 ms |
1104 KB |
Output is correct |
14 |
Correct |
1 ms |
1104 KB |
Output is correct |
15 |
Correct |
2 ms |
1272 KB |
Output is correct |
16 |
Correct |
2 ms |
1104 KB |
Output is correct |
17 |
Correct |
2 ms |
1104 KB |
Output is correct |
18 |
Correct |
4 ms |
1104 KB |
Output is correct |
19 |
Correct |
4 ms |
1280 KB |
Output is correct |
20 |
Correct |
5 ms |
1104 KB |
Output is correct |
21 |
Correct |
2 ms |
1276 KB |
Output is correct |
22 |
Correct |
3 ms |
1104 KB |
Output is correct |
23 |
Correct |
3 ms |
1104 KB |
Output is correct |
24 |
Correct |
5 ms |
1228 KB |
Output is correct |
25 |
Correct |
3 ms |
1104 KB |
Output is correct |
26 |
Correct |
4 ms |
1104 KB |
Output is correct |
27 |
Correct |
4 ms |
1104 KB |
Output is correct |
28 |
Correct |
4 ms |
1272 KB |
Output is correct |
29 |
Correct |
4 ms |
1104 KB |
Output is correct |
30 |
Correct |
4 ms |
1104 KB |
Output is correct |
31 |
Correct |
3 ms |
1104 KB |
Output is correct |
32 |
Correct |
4 ms |
1104 KB |
Output is correct |
33 |
Correct |
4 ms |
1104 KB |
Output is correct |
34 |
Correct |
4 ms |
1104 KB |
Output is correct |
35 |
Correct |
6 ms |
1360 KB |
Output is correct |
36 |
Correct |
2 ms |
1104 KB |
Output is correct |
37 |
Correct |
9 ms |
1360 KB |
Output is correct |
38 |
Correct |
7 ms |
1360 KB |
Output is correct |
39 |
Correct |
5 ms |
1404 KB |
Output is correct |
40 |
Correct |
5 ms |
1616 KB |
Output is correct |
41 |
Correct |
6 ms |
1360 KB |
Output is correct |
42 |
Correct |
6 ms |
1360 KB |
Output is correct |
43 |
Correct |
6 ms |
1360 KB |
Output is correct |
44 |
Correct |
47 ms |
1384 KB |
Output is correct |
45 |
Correct |
7 ms |
1360 KB |
Output is correct |
46 |
Correct |
7 ms |
1376 KB |
Output is correct |
47 |
Correct |
51 ms |
1784 KB |
Output is correct |
48 |
Correct |
270 ms |
1616 KB |
Output is correct |
49 |
Correct |
313 ms |
1864 KB |
Output is correct |
50 |
Correct |
389 ms |
1616 KB |
Output is correct |
51 |
Correct |
496 ms |
1872 KB |
Output is correct |
52 |
Correct |
489 ms |
2320 KB |
Output is correct |
53 |
Correct |
72 ms |
2256 KB |
Output is correct |
54 |
Correct |
416 ms |
1368 KB |
Output is correct |
55 |
Correct |
411 ms |
1352 KB |
Output is correct |
56 |
Correct |
859 ms |
2448 KB |
Output is correct |
57 |
Correct |
8 ms |
1360 KB |
Output is correct |
58 |
Correct |
541 ms |
1616 KB |
Output is correct |
59 |
Correct |
438 ms |
1864 KB |
Output is correct |
60 |
Correct |
432 ms |
1860 KB |
Output is correct |
61 |
Correct |
442 ms |
1864 KB |
Output is correct |
62 |
Correct |
554 ms |
2640 KB |
Output is correct |
63 |
Correct |
450 ms |
1864 KB |
Output is correct |
64 |
Correct |
436 ms |
1736 KB |
Output is correct |
65 |
Correct |
433 ms |
1824 KB |
Output is correct |
66 |
Correct |
446 ms |
1772 KB |
Output is correct |
67 |
Correct |
453 ms |
1812 KB |
Output is correct |