#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;
ll binpow(ll a, ll p, int mod = MOD) {
ll res = 1;
while (p) {
if (p & 1) {
(res *= a) %= mod;
}
p >>= 1;
(a *= a) %= mod;
}
return res;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
const int N = 30005;
const int M = 100;
int dist[N][M + 1];
set<int> doges[N];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
fill(dist[i], dist[i] + M + 1, 1e9);
}
pair<int, int> start;
int finish;
for (int i = 0; i < m; i++) {
int b, p;
cin >> b >> p;
doges[b].insert(p);
if (i == 0) {
start = {b, M};
dist[b][M] = 0;
}
if (i == 1) {
finish = b;
}
}
set<pair<int, pair<int, int>>> setik;
setik.insert({0, start});
while (!setik.empty()) {
int v, p;
tie(v, p) = setik.begin()->second;
setik.erase(setik.begin());
if (p == M) {
for (int doge : doges[v]) {
if (doge < M) {
if (dist[v][p] < dist[v][doge]) {
setik.erase({dist[v][doge], {v, doge}});
dist[v][doge] = dist[v][p];
setik.insert({dist[v][doge], {v, doge}});
}
} else {
for (int i = v % doge; i < n; i += doge) {
if (dist[v][p] + abs(v - i) / doge < dist[i][M]) {
setik.erase({dist[i][M], {i, M}});
dist[i][M] = dist[v][p] + abs(v - i) / doge;
setik.insert({dist[i][M], {i, M}});
}
}
}
}
continue;
}
if (v + p < n && dist[v][p] + 1 < dist[v + p][p]) {
setik.erase({dist[v + p][p], {v + p, p}});
dist[v + p][p] = dist[v][p] + 1;
setik.insert({dist[v + p][p], {v + p, p}});
}
if (v - p >= 0 && dist[v][p] + 1 < dist[v - p][p]) {
setik.erase({dist[v - p][p], {v - p, p}});
dist[v - p][p] = dist[v][p] + 1;
setik.insert({dist[v - p][p], {v - p, p}});
}
if (dist[v][p] < dist[v][M]) {
setik.erase({dist[v][M], {v, M}});
dist[v][M] = dist[v][p];
setik.insert({dist[v][M], {v, M}});
}
}
int ans = *min_element(dist[finish], dist[finish] + M + 1);
if (ans == 1e9) ans = -1;
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
Compilation message
skyscraper.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:42:9: warning: 'finish' may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | int finish;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
2 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
2 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1732 KB |
Output is correct |
3 |
Correct |
2 ms |
1612 KB |
Output is correct |
4 |
Correct |
2 ms |
1612 KB |
Output is correct |
5 |
Correct |
2 ms |
1612 KB |
Output is correct |
6 |
Correct |
2 ms |
1612 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
8 |
Correct |
2 ms |
1740 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1732 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
2 ms |
1612 KB |
Output is correct |
6 |
Correct |
2 ms |
1612 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
8 |
Correct |
2 ms |
1728 KB |
Output is correct |
9 |
Correct |
2 ms |
1728 KB |
Output is correct |
10 |
Correct |
2 ms |
1796 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1728 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1868 KB |
Output is correct |
16 |
Correct |
2 ms |
1856 KB |
Output is correct |
17 |
Correct |
4 ms |
2120 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2496 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1868 KB |
Output is correct |
22 |
Correct |
2 ms |
2380 KB |
Output is correct |
23 |
Correct |
4 ms |
2380 KB |
Output is correct |
24 |
Correct |
4 ms |
2508 KB |
Output is correct |
25 |
Correct |
4 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
4 ms |
2636 KB |
Output is correct |
29 |
Correct |
12 ms |
2496 KB |
Output is correct |
30 |
Correct |
6 ms |
2508 KB |
Output is correct |
31 |
Correct |
8 ms |
2508 KB |
Output is correct |
32 |
Correct |
7 ms |
2572 KB |
Output is correct |
33 |
Correct |
22 ms |
2752 KB |
Output is correct |
34 |
Correct |
22 ms |
2748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1732 KB |
Output is correct |
4 |
Correct |
1 ms |
1732 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
1 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
7 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1996 KB |
Output is correct |
16 |
Correct |
2 ms |
1772 KB |
Output is correct |
17 |
Correct |
4 ms |
2124 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2508 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1868 KB |
Output is correct |
22 |
Correct |
3 ms |
2380 KB |
Output is correct |
23 |
Correct |
3 ms |
2380 KB |
Output is correct |
24 |
Correct |
4 ms |
2508 KB |
Output is correct |
25 |
Correct |
3 ms |
2508 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
4 ms |
2632 KB |
Output is correct |
29 |
Correct |
14 ms |
2604 KB |
Output is correct |
30 |
Correct |
5 ms |
2492 KB |
Output is correct |
31 |
Correct |
10 ms |
2496 KB |
Output is correct |
32 |
Correct |
8 ms |
2508 KB |
Output is correct |
33 |
Correct |
22 ms |
2752 KB |
Output is correct |
34 |
Correct |
23 ms |
2748 KB |
Output is correct |
35 |
Correct |
25 ms |
3816 KB |
Output is correct |
36 |
Correct |
5 ms |
2368 KB |
Output is correct |
37 |
Correct |
31 ms |
3796 KB |
Output is correct |
38 |
Correct |
30 ms |
4360 KB |
Output is correct |
39 |
Correct |
31 ms |
4276 KB |
Output is correct |
40 |
Correct |
34 ms |
4312 KB |
Output is correct |
41 |
Correct |
31 ms |
4316 KB |
Output is correct |
42 |
Correct |
8 ms |
2636 KB |
Output is correct |
43 |
Correct |
7 ms |
2648 KB |
Output is correct |
44 |
Correct |
7 ms |
2764 KB |
Output is correct |
45 |
Correct |
52 ms |
4504 KB |
Output is correct |
46 |
Correct |
52 ms |
4516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1724 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
2 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
2 ms |
1728 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
3 ms |
1868 KB |
Output is correct |
15 |
Correct |
3 ms |
1868 KB |
Output is correct |
16 |
Correct |
2 ms |
1824 KB |
Output is correct |
17 |
Correct |
4 ms |
2124 KB |
Output is correct |
18 |
Correct |
3 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2492 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1868 KB |
Output is correct |
22 |
Correct |
2 ms |
2380 KB |
Output is correct |
23 |
Correct |
5 ms |
2380 KB |
Output is correct |
24 |
Correct |
4 ms |
2512 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
3 ms |
2504 KB |
Output is correct |
28 |
Correct |
3 ms |
2636 KB |
Output is correct |
29 |
Correct |
12 ms |
2508 KB |
Output is correct |
30 |
Correct |
4 ms |
2508 KB |
Output is correct |
31 |
Correct |
8 ms |
2508 KB |
Output is correct |
32 |
Correct |
7 ms |
2508 KB |
Output is correct |
33 |
Correct |
23 ms |
2752 KB |
Output is correct |
34 |
Correct |
29 ms |
2636 KB |
Output is correct |
35 |
Correct |
24 ms |
3864 KB |
Output is correct |
36 |
Correct |
6 ms |
2380 KB |
Output is correct |
37 |
Correct |
37 ms |
3780 KB |
Output is correct |
38 |
Correct |
39 ms |
4344 KB |
Output is correct |
39 |
Correct |
35 ms |
4352 KB |
Output is correct |
40 |
Correct |
34 ms |
4352 KB |
Output is correct |
41 |
Correct |
44 ms |
4312 KB |
Output is correct |
42 |
Correct |
8 ms |
2636 KB |
Output is correct |
43 |
Correct |
8 ms |
2636 KB |
Output is correct |
44 |
Correct |
9 ms |
2764 KB |
Output is correct |
45 |
Correct |
57 ms |
4556 KB |
Output is correct |
46 |
Correct |
59 ms |
4556 KB |
Output is correct |
47 |
Correct |
165 ms |
8640 KB |
Output is correct |
48 |
Correct |
16 ms |
12108 KB |
Output is correct |
49 |
Correct |
15 ms |
12768 KB |
Output is correct |
50 |
Correct |
15 ms |
13516 KB |
Output is correct |
51 |
Correct |
104 ms |
15932 KB |
Output is correct |
52 |
Correct |
82 ms |
15908 KB |
Output is correct |
53 |
Correct |
30 ms |
15104 KB |
Output is correct |
54 |
Correct |
14 ms |
13496 KB |
Output is correct |
55 |
Correct |
11 ms |
13516 KB |
Output is correct |
56 |
Correct |
27 ms |
15016 KB |
Output is correct |
57 |
Correct |
132 ms |
13752 KB |
Output is correct |
58 |
Correct |
27 ms |
13700 KB |
Output is correct |
59 |
Correct |
24 ms |
13648 KB |
Output is correct |
60 |
Correct |
27 ms |
13728 KB |
Output is correct |
61 |
Correct |
27 ms |
13880 KB |
Output is correct |
62 |
Correct |
52 ms |
15100 KB |
Output is correct |
63 |
Correct |
109 ms |
16896 KB |
Output is correct |
64 |
Correct |
128 ms |
16964 KB |
Output is correct |
65 |
Correct |
158 ms |
16972 KB |
Output is correct |
66 |
Correct |
687 ms |
17416 KB |
Output is correct |
67 |
Correct |
667 ms |
17364 KB |
Output is correct |