#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
template<class... Args>
vec(size_t n = 0, Args... args)
: vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
template<class... Args>
vec(Args... args)
: vector<T>(args...) {}
};
template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }
mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n_skyscrapers, n_doges; cin >> n_skyscrapers >> n_doges;
vec<int, 2> steps_skyscrapers(n_skyscrapers);
int source, target;
for (int i = 0; i < n_doges; ++i) {
int skyscraper, step; cin >> skyscraper >> step;
steps_skyscrapers[skyscraper].emplace_back(step);
if (!i) {
source = skyscraper;
} else if (i == 1) {
target = skyscraper;
}
}
for (auto& doges : steps_skyscrapers) {
sort(ALL(doges));
doges.erase(unique(ALL(doges)), doges.end());
}
int const magic = static_cast<int>(sqrt(n_skyscrapers)) + 1;
vec<int, 2> f(n_skyscrapers, magic + 1, numeric_limits<int>::max());
f[source].back() = 0;
priority_queue<tuple<int, int, int>> pq;
pq.emplace(0, source, magic);
while (SZ(pq)) {
int current_f, skyscraper, step; tie(current_f, skyscraper, step) = pq.top(); pq.pop();
current_f = -current_f;
if (current_f != f[skyscraper][step]) {
continue;
}
if (step < magic) {
if (skyscraper + step < n_skyscrapers && Minimize(f[skyscraper + step][step], current_f + 1)) {
pq.emplace(-f[skyscraper + step][step], skyscraper + step, step);
}
if (skyscraper - step >= 0 && Minimize(f[skyscraper - step][step], current_f + 1)) {
pq.emplace(-f[skyscraper - step][step], skyscraper - step, step);
}
if (Minimize(f[skyscraper][magic], current_f)) {
pq.emplace(-f[skyscraper][magic], skyscraper, magic);
}
} else {
for (auto& new_step : steps_skyscrapers[skyscraper]) {
if (new_step < magic) {
if (Minimize(f[skyscraper][new_step], current_f)) {
pq.emplace(-f[skyscraper][new_step], skyscraper, new_step);
}
} else {
for (int next_skyscraper = skyscraper + new_step, next_distance = current_f + 1; next_skyscraper < n_skyscrapers; next_skyscraper += new_step, ++next_distance) {
if (Minimize(f[next_skyscraper][magic], next_distance)) {
pq.emplace(-f[next_skyscraper][magic], next_skyscraper, magic);
}
}
for (int next_skyscraper = skyscraper - new_step, next_distance = current_f + 1; next_skyscraper >= 0; next_skyscraper -= new_step, ++next_distance) {
if (Minimize(f[next_skyscraper][magic], next_distance)) {
pq.emplace(-f[next_skyscraper][magic], next_skyscraper, magic);
}
}
}
}
}
}
int answer = *min_element(ALL(f[target]));
cout << (answer < numeric_limits<int>::max() ? answer : -1) << '\n';
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:96:41: warning: 'target' may be used uninitialized in this function [-Wmaybe-uninitialized]
int answer = *min_element(ALL(f[target]));
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
768 KB |
Output is correct |
19 |
Correct |
1 ms |
768 KB |
Output is correct |
20 |
Correct |
1 ms |
896 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
896 KB |
Output is correct |
25 |
Correct |
3 ms |
896 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
768 KB |
Output is correct |
28 |
Correct |
2 ms |
896 KB |
Output is correct |
29 |
Correct |
8 ms |
768 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
6 ms |
896 KB |
Output is correct |
32 |
Correct |
3 ms |
896 KB |
Output is correct |
33 |
Correct |
11 ms |
1040 KB |
Output is correct |
34 |
Correct |
12 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
768 KB |
Output is correct |
19 |
Correct |
2 ms |
768 KB |
Output is correct |
20 |
Correct |
2 ms |
896 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
896 KB |
Output is correct |
25 |
Correct |
2 ms |
896 KB |
Output is correct |
26 |
Correct |
2 ms |
928 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
2 ms |
896 KB |
Output is correct |
29 |
Correct |
8 ms |
896 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
6 ms |
896 KB |
Output is correct |
32 |
Correct |
3 ms |
896 KB |
Output is correct |
33 |
Correct |
11 ms |
1024 KB |
Output is correct |
34 |
Correct |
12 ms |
1024 KB |
Output is correct |
35 |
Correct |
12 ms |
1152 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
16 ms |
1152 KB |
Output is correct |
38 |
Correct |
16 ms |
1408 KB |
Output is correct |
39 |
Correct |
15 ms |
1408 KB |
Output is correct |
40 |
Correct |
15 ms |
1408 KB |
Output is correct |
41 |
Correct |
16 ms |
1408 KB |
Output is correct |
42 |
Correct |
6 ms |
1152 KB |
Output is correct |
43 |
Correct |
6 ms |
1152 KB |
Output is correct |
44 |
Correct |
6 ms |
1152 KB |
Output is correct |
45 |
Correct |
20 ms |
1280 KB |
Output is correct |
46 |
Correct |
18 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
768 KB |
Output is correct |
19 |
Correct |
1 ms |
768 KB |
Output is correct |
20 |
Correct |
1 ms |
896 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
896 KB |
Output is correct |
25 |
Correct |
2 ms |
896 KB |
Output is correct |
26 |
Correct |
1 ms |
768 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
2 ms |
896 KB |
Output is correct |
29 |
Correct |
8 ms |
896 KB |
Output is correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Correct |
5 ms |
896 KB |
Output is correct |
32 |
Correct |
3 ms |
896 KB |
Output is correct |
33 |
Correct |
13 ms |
1024 KB |
Output is correct |
34 |
Correct |
12 ms |
1024 KB |
Output is correct |
35 |
Correct |
12 ms |
1152 KB |
Output is correct |
36 |
Correct |
3 ms |
640 KB |
Output is correct |
37 |
Correct |
13 ms |
1152 KB |
Output is correct |
38 |
Correct |
16 ms |
1408 KB |
Output is correct |
39 |
Correct |
18 ms |
1408 KB |
Output is correct |
40 |
Correct |
16 ms |
1408 KB |
Output is correct |
41 |
Correct |
15 ms |
1408 KB |
Output is correct |
42 |
Correct |
6 ms |
1152 KB |
Output is correct |
43 |
Correct |
6 ms |
1152 KB |
Output is correct |
44 |
Correct |
6 ms |
1152 KB |
Output is correct |
45 |
Correct |
19 ms |
1408 KB |
Output is correct |
46 |
Correct |
18 ms |
1312 KB |
Output is correct |
47 |
Correct |
102 ms |
7616 KB |
Output is correct |
48 |
Correct |
17 ms |
16256 KB |
Output is correct |
49 |
Correct |
19 ms |
18484 KB |
Output is correct |
50 |
Correct |
18 ms |
21048 KB |
Output is correct |
51 |
Correct |
66 ms |
24376 KB |
Output is correct |
52 |
Correct |
73 ms |
24376 KB |
Output is correct |
53 |
Correct |
31 ms |
23912 KB |
Output is correct |
54 |
Correct |
17 ms |
22896 KB |
Output is correct |
55 |
Correct |
15 ms |
22912 KB |
Output is correct |
56 |
Correct |
25 ms |
23972 KB |
Output is correct |
57 |
Correct |
98 ms |
23040 KB |
Output is correct |
58 |
Correct |
21 ms |
23160 KB |
Output is correct |
59 |
Correct |
28 ms |
23296 KB |
Output is correct |
60 |
Correct |
43 ms |
23160 KB |
Output is correct |
61 |
Correct |
27 ms |
23288 KB |
Output is correct |
62 |
Correct |
46 ms |
24192 KB |
Output is correct |
63 |
Correct |
74 ms |
24312 KB |
Output is correct |
64 |
Correct |
85 ms |
24088 KB |
Output is correct |
65 |
Correct |
339 ms |
24952 KB |
Output is correct |
66 |
Correct |
862 ms |
24948 KB |
Output is correct |
67 |
Correct |
883 ms |
24948 KB |
Output is correct |