#include <bits/stdc++.h>
template <class T>
using Vec = std::vector<T>;
template <class T>
using Heap = std::priority_queue<T, Vec<T>, std::greater<T>>;
constexpr int INF = std::numeric_limits<int>::max() / 2;
constexpr int SQRT = 150;
int main() {
int N, M;
std::cin >> N >> M;
Vec<int> B(M), P(M);
Vec<Vec<int>> lives(N);
for (int i = 0; i < M; ++i) {
std::cin >> B[i] >> P[i];
lives[B[i]].push_back(P[i]);
}
for (int i = 0; i < N; ++i) {
std::sort(lives[i].begin(), lives[i].end());
lives[i].erase(std::unique(lives[i].begin(), lives[i].end()), lives[i].end());
}
Vec<std::array<int, SQRT + 1>> dist(N);
for (auto &arr: dist) {
arr.fill(INF);
}
Heap<std::tuple<int, int, int>> heap;
const auto push = [&](const int u, const int k, const int d) {
if (dist[u][k] > d) {
dist[u][k] = d;
heap.emplace(d, u, k);
}
};
push(B[0], 0, 0);
while (!heap.empty()) {
const auto [d, u, k] = heap.top();
heap.pop();
if (dist[u][k] < d) {
continue;
}
if (k == 0) {
for (const auto p: lives[u]) {
if (p <= SQRT) {
push(u, p, d);
}
else {
for (int i = 1; u + i * p < N; ++i) {
push(u + i * p, 0, d + i);
}
for (int i = 1; u - i * p >= 0; ++i) {
push(u - i * p, 0, d + i);
}
}
}
}
else {
push(u, 0, d);
if (u + k < N) {
push(u + k, k, d + 1);
}
if (u - k >= 0) {
push(u - k, k, d + 1);
}
}
}
const auto ans = dist[B[1]][0];
if (ans == INF) {
std::cout << -1 << '\n';
}
else {
std::cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
4 ms |
876 KB |
Output is correct |
18 |
Correct |
2 ms |
1388 KB |
Output is correct |
19 |
Correct |
2 ms |
1516 KB |
Output is correct |
20 |
Correct |
3 ms |
1644 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
3 ms |
1388 KB |
Output is correct |
24 |
Correct |
4 ms |
1516 KB |
Output is correct |
25 |
Correct |
4 ms |
1644 KB |
Output is correct |
26 |
Correct |
3 ms |
1516 KB |
Output is correct |
27 |
Correct |
3 ms |
1516 KB |
Output is correct |
28 |
Correct |
3 ms |
1644 KB |
Output is correct |
29 |
Correct |
9 ms |
1516 KB |
Output is correct |
30 |
Correct |
13 ms |
1516 KB |
Output is correct |
31 |
Correct |
7 ms |
1516 KB |
Output is correct |
32 |
Correct |
5 ms |
1516 KB |
Output is correct |
33 |
Correct |
18 ms |
1644 KB |
Output is correct |
34 |
Correct |
18 ms |
1644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
4 ms |
876 KB |
Output is correct |
18 |
Correct |
2 ms |
1388 KB |
Output is correct |
19 |
Correct |
2 ms |
1516 KB |
Output is correct |
20 |
Correct |
3 ms |
1644 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
3 ms |
1388 KB |
Output is correct |
24 |
Correct |
4 ms |
1516 KB |
Output is correct |
25 |
Correct |
3 ms |
1644 KB |
Output is correct |
26 |
Correct |
3 ms |
1516 KB |
Output is correct |
27 |
Correct |
3 ms |
1516 KB |
Output is correct |
28 |
Correct |
4 ms |
1644 KB |
Output is correct |
29 |
Correct |
9 ms |
1516 KB |
Output is correct |
30 |
Correct |
4 ms |
1516 KB |
Output is correct |
31 |
Correct |
6 ms |
1516 KB |
Output is correct |
32 |
Correct |
5 ms |
1516 KB |
Output is correct |
33 |
Correct |
18 ms |
1644 KB |
Output is correct |
34 |
Correct |
17 ms |
1644 KB |
Output is correct |
35 |
Correct |
24 ms |
1900 KB |
Output is correct |
36 |
Correct |
5 ms |
1132 KB |
Output is correct |
37 |
Correct |
28 ms |
2156 KB |
Output is correct |
38 |
Correct |
32 ms |
2412 KB |
Output is correct |
39 |
Correct |
32 ms |
2412 KB |
Output is correct |
40 |
Correct |
32 ms |
2412 KB |
Output is correct |
41 |
Correct |
31 ms |
2412 KB |
Output is correct |
42 |
Correct |
16 ms |
2284 KB |
Output is correct |
43 |
Correct |
16 ms |
2284 KB |
Output is correct |
44 |
Correct |
16 ms |
2156 KB |
Output is correct |
45 |
Correct |
62 ms |
2540 KB |
Output is correct |
46 |
Correct |
61 ms |
2540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
3 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
4 ms |
876 KB |
Output is correct |
18 |
Correct |
2 ms |
1388 KB |
Output is correct |
19 |
Correct |
2 ms |
1516 KB |
Output is correct |
20 |
Correct |
3 ms |
1644 KB |
Output is correct |
21 |
Correct |
1 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
1388 KB |
Output is correct |
23 |
Correct |
3 ms |
1388 KB |
Output is correct |
24 |
Correct |
4 ms |
1516 KB |
Output is correct |
25 |
Correct |
3 ms |
1644 KB |
Output is correct |
26 |
Correct |
3 ms |
1516 KB |
Output is correct |
27 |
Correct |
3 ms |
1516 KB |
Output is correct |
28 |
Correct |
4 ms |
1644 KB |
Output is correct |
29 |
Correct |
9 ms |
1516 KB |
Output is correct |
30 |
Correct |
4 ms |
1516 KB |
Output is correct |
31 |
Correct |
6 ms |
1516 KB |
Output is correct |
32 |
Correct |
5 ms |
1516 KB |
Output is correct |
33 |
Correct |
17 ms |
1644 KB |
Output is correct |
34 |
Correct |
18 ms |
1644 KB |
Output is correct |
35 |
Correct |
25 ms |
1900 KB |
Output is correct |
36 |
Correct |
5 ms |
1132 KB |
Output is correct |
37 |
Correct |
29 ms |
2156 KB |
Output is correct |
38 |
Correct |
32 ms |
2540 KB |
Output is correct |
39 |
Correct |
34 ms |
2540 KB |
Output is correct |
40 |
Correct |
31 ms |
2412 KB |
Output is correct |
41 |
Correct |
31 ms |
2412 KB |
Output is correct |
42 |
Correct |
16 ms |
2228 KB |
Output is correct |
43 |
Correct |
16 ms |
2284 KB |
Output is correct |
44 |
Correct |
16 ms |
2156 KB |
Output is correct |
45 |
Correct |
63 ms |
2540 KB |
Output is correct |
46 |
Correct |
61 ms |
2540 KB |
Output is correct |
47 |
Correct |
107 ms |
9132 KB |
Output is correct |
48 |
Correct |
30 ms |
15468 KB |
Output is correct |
49 |
Correct |
30 ms |
16876 KB |
Output is correct |
50 |
Correct |
26 ms |
18156 KB |
Output is correct |
51 |
Correct |
81 ms |
20396 KB |
Output is correct |
52 |
Correct |
89 ms |
20400 KB |
Output is correct |
53 |
Correct |
46 ms |
19948 KB |
Output is correct |
54 |
Correct |
19 ms |
18796 KB |
Output is correct |
55 |
Correct |
18 ms |
18796 KB |
Output is correct |
56 |
Correct |
37 ms |
20204 KB |
Output is correct |
57 |
Correct |
100 ms |
18924 KB |
Output is correct |
58 |
Correct |
32 ms |
19308 KB |
Output is correct |
59 |
Correct |
37 ms |
19436 KB |
Output is correct |
60 |
Correct |
42 ms |
19436 KB |
Output is correct |
61 |
Correct |
40 ms |
19436 KB |
Output is correct |
62 |
Correct |
62 ms |
20204 KB |
Output is correct |
63 |
Correct |
80 ms |
21220 KB |
Output is correct |
64 |
Correct |
86 ms |
21096 KB |
Output is correct |
65 |
Correct |
275 ms |
22628 KB |
Output is correct |
66 |
Correct |
818 ms |
22628 KB |
Output is correct |
67 |
Correct |
834 ms |
22624 KB |
Output is correct |