#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
const int N = 30010;
const int oo = 1e8;
using ii = pair<int,int>;
int n, m, st, ft;
vector<int> f[N];
int dist[N];
vector<ii> g[N];
priority_queue<ii, vector<ii>, greater<ii> > q;
bool has(int b, int p) {
int lo = 0, hi = (int)f[b].size() - 1;
while(lo <= hi) {
int mid = (lo + hi) >> 1;
if(f[b][mid] == p) return true;
if(f[b][mid] < p) lo = mid+1;
else hi = mid-1;
} return false;
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 0; i < m; ++i) {
int b, p; scanf("%d %d", &b, &p);
f[b].push_back(p);
if(i == 0) st = b;
else if(i == 1) ft = b;
}
for(int i = 0; i < n; ++i) {
sort(f[i].begin(), f[i].end());
f[i].erase(unique(f[i].begin(),f[i].end()), f[i].end());
}
for(int i = 0; i < n; ++i) {
for(int p : f[i]) {
for(int x = i + p, c = 1; x < n; x += p, ++c) {
g[i].push_back({c, x});
if(has(x,p)) break;
}
for(int x = i - p, c = 1; x >= 0; x -= p, ++c) {
g[i].push_back({c, x});
if(has(x,p)) break;
}
}
}
for(int i = 0; i < n; ++i) dist[i] = oo;
q.push({0,st});
dist[st] = 0;
while(!q.empty()) {
auto U = q.top(); q.pop();
int cw = U.first, u = U.second;
for(auto e : g[u]) {
if(dist[e.second] > cw + e.first) {
dist[e.second] = cw + e.first;
q.push({cw + e.first, e.second});
}
}
}
if(dist[ft] == oo) puts("-1");
else printf("%d\n", dist[ft]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:29: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:31:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int b, p; scanf("%d %d", &b, &p);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1792 KB |
Output is correct |
2 |
Correct |
3 ms |
1664 KB |
Output is correct |
3 |
Correct |
4 ms |
1792 KB |
Output is correct |
4 |
Correct |
3 ms |
1792 KB |
Output is correct |
5 |
Correct |
5 ms |
1792 KB |
Output is correct |
6 |
Correct |
4 ms |
1792 KB |
Output is correct |
7 |
Correct |
4 ms |
1792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1792 KB |
Output is correct |
2 |
Correct |
5 ms |
1792 KB |
Output is correct |
3 |
Correct |
3 ms |
1792 KB |
Output is correct |
4 |
Correct |
3 ms |
1792 KB |
Output is correct |
5 |
Correct |
3 ms |
1664 KB |
Output is correct |
6 |
Correct |
5 ms |
1792 KB |
Output is correct |
7 |
Correct |
4 ms |
1792 KB |
Output is correct |
8 |
Correct |
3 ms |
1792 KB |
Output is correct |
9 |
Correct |
4 ms |
1792 KB |
Output is correct |
10 |
Correct |
4 ms |
1792 KB |
Output is correct |
11 |
Correct |
4 ms |
1792 KB |
Output is correct |
12 |
Correct |
5 ms |
1792 KB |
Output is correct |
13 |
Correct |
4 ms |
1792 KB |
Output is correct |
14 |
Correct |
4 ms |
1792 KB |
Output is correct |
15 |
Correct |
4 ms |
1792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1664 KB |
Output is correct |
2 |
Correct |
5 ms |
1664 KB |
Output is correct |
3 |
Correct |
5 ms |
1792 KB |
Output is correct |
4 |
Correct |
3 ms |
1664 KB |
Output is correct |
5 |
Correct |
3 ms |
1664 KB |
Output is correct |
6 |
Correct |
3 ms |
1664 KB |
Output is correct |
7 |
Correct |
4 ms |
1792 KB |
Output is correct |
8 |
Correct |
3 ms |
1664 KB |
Output is correct |
9 |
Correct |
3 ms |
1664 KB |
Output is correct |
10 |
Correct |
3 ms |
1792 KB |
Output is correct |
11 |
Correct |
4 ms |
1792 KB |
Output is correct |
12 |
Correct |
4 ms |
1792 KB |
Output is correct |
13 |
Correct |
4 ms |
1792 KB |
Output is correct |
14 |
Correct |
5 ms |
1792 KB |
Output is correct |
15 |
Correct |
6 ms |
1792 KB |
Output is correct |
16 |
Correct |
4 ms |
1792 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
4 ms |
1792 KB |
Output is correct |
19 |
Correct |
4 ms |
1792 KB |
Output is correct |
20 |
Correct |
4 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1792 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
4 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1792 KB |
Output is correct |
27 |
Correct |
4 ms |
1792 KB |
Output is correct |
28 |
Correct |
5 ms |
2048 KB |
Output is correct |
29 |
Correct |
6 ms |
2560 KB |
Output is correct |
30 |
Correct |
6 ms |
2048 KB |
Output is correct |
31 |
Correct |
7 ms |
2176 KB |
Output is correct |
32 |
Correct |
5 ms |
2048 KB |
Output is correct |
33 |
Correct |
9 ms |
3200 KB |
Output is correct |
34 |
Correct |
9 ms |
3200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1792 KB |
Output is correct |
2 |
Correct |
3 ms |
1792 KB |
Output is correct |
3 |
Correct |
3 ms |
1792 KB |
Output is correct |
4 |
Correct |
4 ms |
1792 KB |
Output is correct |
5 |
Correct |
4 ms |
1792 KB |
Output is correct |
6 |
Correct |
4 ms |
1664 KB |
Output is correct |
7 |
Correct |
4 ms |
1792 KB |
Output is correct |
8 |
Correct |
4 ms |
1792 KB |
Output is correct |
9 |
Correct |
4 ms |
1792 KB |
Output is correct |
10 |
Correct |
3 ms |
1792 KB |
Output is correct |
11 |
Correct |
4 ms |
1792 KB |
Output is correct |
12 |
Correct |
5 ms |
1792 KB |
Output is correct |
13 |
Correct |
4 ms |
1792 KB |
Output is correct |
14 |
Correct |
5 ms |
1792 KB |
Output is correct |
15 |
Correct |
4 ms |
1792 KB |
Output is correct |
16 |
Correct |
4 ms |
1664 KB |
Output is correct |
17 |
Correct |
4 ms |
1920 KB |
Output is correct |
18 |
Correct |
4 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
1792 KB |
Output is correct |
20 |
Correct |
5 ms |
1920 KB |
Output is correct |
21 |
Correct |
4 ms |
1792 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
1792 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
5 ms |
1920 KB |
Output is correct |
27 |
Correct |
4 ms |
1792 KB |
Output is correct |
28 |
Correct |
4 ms |
2048 KB |
Output is correct |
29 |
Correct |
7 ms |
2560 KB |
Output is correct |
30 |
Correct |
5 ms |
2048 KB |
Output is correct |
31 |
Correct |
6 ms |
2176 KB |
Output is correct |
32 |
Correct |
5 ms |
2048 KB |
Output is correct |
33 |
Correct |
9 ms |
3200 KB |
Output is correct |
34 |
Correct |
9 ms |
3328 KB |
Output is correct |
35 |
Correct |
15 ms |
2944 KB |
Output is correct |
36 |
Correct |
6 ms |
2048 KB |
Output is correct |
37 |
Correct |
19 ms |
3968 KB |
Output is correct |
38 |
Correct |
21 ms |
3840 KB |
Output is correct |
39 |
Correct |
18 ms |
3836 KB |
Output is correct |
40 |
Correct |
23 ms |
3840 KB |
Output is correct |
41 |
Correct |
20 ms |
3840 KB |
Output is correct |
42 |
Correct |
15 ms |
2048 KB |
Output is correct |
43 |
Correct |
11 ms |
2048 KB |
Output is correct |
44 |
Correct |
10 ms |
2048 KB |
Output is correct |
45 |
Correct |
24 ms |
6520 KB |
Output is correct |
46 |
Correct |
26 ms |
6528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1792 KB |
Output is correct |
2 |
Correct |
4 ms |
1792 KB |
Output is correct |
3 |
Correct |
3 ms |
1664 KB |
Output is correct |
4 |
Correct |
4 ms |
1792 KB |
Output is correct |
5 |
Correct |
3 ms |
1792 KB |
Output is correct |
6 |
Correct |
3 ms |
1764 KB |
Output is correct |
7 |
Correct |
4 ms |
1792 KB |
Output is correct |
8 |
Correct |
3 ms |
1792 KB |
Output is correct |
9 |
Correct |
3 ms |
1664 KB |
Output is correct |
10 |
Correct |
4 ms |
1792 KB |
Output is correct |
11 |
Correct |
4 ms |
1792 KB |
Output is correct |
12 |
Correct |
4 ms |
1792 KB |
Output is correct |
13 |
Correct |
4 ms |
1792 KB |
Output is correct |
14 |
Correct |
4 ms |
1792 KB |
Output is correct |
15 |
Correct |
4 ms |
1792 KB |
Output is correct |
16 |
Correct |
4 ms |
1792 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
4 ms |
1920 KB |
Output is correct |
19 |
Correct |
3 ms |
1792 KB |
Output is correct |
20 |
Correct |
4 ms |
1920 KB |
Output is correct |
21 |
Correct |
4 ms |
1792 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
1792 KB |
Output is correct |
24 |
Correct |
6 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1920 KB |
Output is correct |
27 |
Correct |
5 ms |
1792 KB |
Output is correct |
28 |
Correct |
6 ms |
2048 KB |
Output is correct |
29 |
Correct |
6 ms |
2560 KB |
Output is correct |
30 |
Correct |
5 ms |
2048 KB |
Output is correct |
31 |
Correct |
5 ms |
2176 KB |
Output is correct |
32 |
Correct |
4 ms |
2048 KB |
Output is correct |
33 |
Correct |
8 ms |
3200 KB |
Output is correct |
34 |
Correct |
8 ms |
3328 KB |
Output is correct |
35 |
Correct |
17 ms |
3072 KB |
Output is correct |
36 |
Correct |
8 ms |
2048 KB |
Output is correct |
37 |
Correct |
17 ms |
3968 KB |
Output is correct |
38 |
Correct |
21 ms |
3840 KB |
Output is correct |
39 |
Correct |
20 ms |
3832 KB |
Output is correct |
40 |
Correct |
23 ms |
3832 KB |
Output is correct |
41 |
Correct |
25 ms |
3832 KB |
Output is correct |
42 |
Correct |
11 ms |
2048 KB |
Output is correct |
43 |
Correct |
11 ms |
2020 KB |
Output is correct |
44 |
Correct |
17 ms |
1920 KB |
Output is correct |
45 |
Correct |
23 ms |
6528 KB |
Output is correct |
46 |
Correct |
22 ms |
6528 KB |
Output is correct |
47 |
Correct |
59 ms |
11236 KB |
Output is correct |
48 |
Correct |
20 ms |
4724 KB |
Output is correct |
49 |
Correct |
18 ms |
3712 KB |
Output is correct |
50 |
Correct |
18 ms |
3704 KB |
Output is correct |
51 |
Correct |
63 ms |
6484 KB |
Output is correct |
52 |
Correct |
50 ms |
6844 KB |
Output is correct |
53 |
Correct |
25 ms |
3964 KB |
Output is correct |
54 |
Correct |
7 ms |
2304 KB |
Output is correct |
55 |
Correct |
7 ms |
2812 KB |
Output is correct |
56 |
Correct |
16 ms |
3712 KB |
Output is correct |
57 |
Correct |
50 ms |
10884 KB |
Output is correct |
58 |
Correct |
19 ms |
2936 KB |
Output is correct |
59 |
Correct |
23 ms |
3324 KB |
Output is correct |
60 |
Correct |
17 ms |
4092 KB |
Output is correct |
61 |
Correct |
15 ms |
3200 KB |
Output is correct |
62 |
Correct |
39 ms |
7796 KB |
Output is correct |
63 |
Correct |
76 ms |
24528 KB |
Output is correct |
64 |
Correct |
84 ms |
27508 KB |
Output is correct |
65 |
Correct |
117 ms |
37616 KB |
Output is correct |
66 |
Correct |
368 ms |
65892 KB |
Output is correct |
67 |
Correct |
257 ms |
65844 KB |
Output is correct |