#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
const int INF = 1e9;
const int BLK = 200;
int n, m;
vector<int> b, p;
vector<vector<int>> v;
void addDoge(int i, int pos, int d, vector<vector<int>> &dist, priority_queue<tuple<int, int, int>> &q) {
if (p[i] < BLK and -d < dist[pos][p[i]]) {
dist[pos][p[i]] = -d;
q.push(make_tuple(d, pos, p[i]));
} else if (p[i] >= BLK) {
int l = b[i] - p[i], r = b[i] + p[i], x = 1;
while (l >= 0 or r < n) {
if (l >= 0 and -d + x < dist[l][0]) {
dist[l][0] = -d + x;
q.push(make_tuple(d - x, l, 0));
}
if (r < n and -d + x < dist[r][0]) {
dist[r][0] = -d + x;
q.push(make_tuple(d - x, r, 0));
}
l -= p[i];
r += p[i];
x++;
}
}
}
int solve() {
vector<vector<int>> dist(n, vector<int>(BLK, INF));
priority_queue<tuple<int, int, int>> q;
dist[b[0]][p[0]] = 0;
q.push(make_tuple(0, b[0], p[0]));
while (not q.empty()) {
auto [d, pos, power] = q.top();
q.pop();
if (dist[pos][power] != -d) continue;
while (not v[pos].empty()) {
int i = v[pos].back();
addDoge(i, pos, d, dist, q);
v[pos].pop_back();
}
if (pos + power < n and -d + 1 < dist[pos + power][power]) {
dist[pos + power][power] = -d + 1;
q.push(make_tuple(d - 1, pos + power, power));
}
if (pos - power >= 0 and -d + 1 < dist[pos - power][power]) {
dist[pos - power][power] = -d + 1;
q.push(make_tuple(d - 1, pos - power, power));
}
}
int ret = INF;
for (int i = 0; i < BLK; i++) {
ret = min(ret, dist[b[1]][i]);
}
return ret == INF ? -1 : ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
v.resize(n), b.resize(m), p.resize(m);
cin >> b[0] >> p[0];
for (int i = 1; i < m; i++) {
cin >> b[i] >> p[i];
v[b[i]].push_back(i);
}
cout << solve() << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
2136 KB |
Output is correct |
20 |
Correct |
1 ms |
2140 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1628 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
2140 KB |
Output is correct |
26 |
Correct |
1 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
6 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
4 ms |
2392 KB |
Output is correct |
32 |
Correct |
3 ms |
2140 KB |
Output is correct |
33 |
Correct |
12 ms |
2140 KB |
Output is correct |
34 |
Correct |
12 ms |
2228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
556 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
432 KB |
Output is correct |
15 |
Correct |
1 ms |
856 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
1 ms |
2140 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1736 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
2 ms |
2140 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
6 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
2136 KB |
Output is correct |
31 |
Correct |
4 ms |
2140 KB |
Output is correct |
32 |
Correct |
3 ms |
2140 KB |
Output is correct |
33 |
Correct |
12 ms |
2140 KB |
Output is correct |
34 |
Correct |
12 ms |
2140 KB |
Output is correct |
35 |
Correct |
12 ms |
2140 KB |
Output is correct |
36 |
Correct |
3 ms |
1372 KB |
Output is correct |
37 |
Correct |
18 ms |
2608 KB |
Output is correct |
38 |
Correct |
16 ms |
2652 KB |
Output is correct |
39 |
Correct |
16 ms |
2712 KB |
Output is correct |
40 |
Correct |
16 ms |
2704 KB |
Output is correct |
41 |
Correct |
16 ms |
2652 KB |
Output is correct |
42 |
Correct |
4 ms |
2396 KB |
Output is correct |
43 |
Correct |
4 ms |
2396 KB |
Output is correct |
44 |
Correct |
4 ms |
2404 KB |
Output is correct |
45 |
Correct |
50 ms |
3100 KB |
Output is correct |
46 |
Correct |
51 ms |
3100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
556 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
1 ms |
2140 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
1 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
1 ms |
2140 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
6 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
4 ms |
2140 KB |
Output is correct |
32 |
Correct |
3 ms |
2140 KB |
Output is correct |
33 |
Correct |
12 ms |
2140 KB |
Output is correct |
34 |
Correct |
12 ms |
2480 KB |
Output is correct |
35 |
Correct |
11 ms |
2244 KB |
Output is correct |
36 |
Correct |
3 ms |
1368 KB |
Output is correct |
37 |
Correct |
17 ms |
2376 KB |
Output is correct |
38 |
Correct |
16 ms |
2648 KB |
Output is correct |
39 |
Correct |
16 ms |
2652 KB |
Output is correct |
40 |
Correct |
16 ms |
2648 KB |
Output is correct |
41 |
Correct |
16 ms |
2652 KB |
Output is correct |
42 |
Correct |
4 ms |
2396 KB |
Output is correct |
43 |
Correct |
4 ms |
2396 KB |
Output is correct |
44 |
Correct |
4 ms |
2396 KB |
Output is correct |
45 |
Correct |
50 ms |
3096 KB |
Output is correct |
46 |
Correct |
50 ms |
3100 KB |
Output is correct |
47 |
Correct |
63 ms |
11804 KB |
Output is correct |
48 |
Correct |
13 ms |
20572 KB |
Output is correct |
49 |
Correct |
13 ms |
22364 KB |
Output is correct |
50 |
Correct |
13 ms |
24408 KB |
Output is correct |
51 |
Correct |
54 ms |
27156 KB |
Output is correct |
52 |
Correct |
50 ms |
27164 KB |
Output is correct |
53 |
Correct |
20 ms |
26460 KB |
Output is correct |
54 |
Correct |
10 ms |
25692 KB |
Output is correct |
55 |
Correct |
11 ms |
25620 KB |
Output is correct |
56 |
Correct |
19 ms |
26800 KB |
Output is correct |
57 |
Correct |
60 ms |
25692 KB |
Output is correct |
58 |
Correct |
15 ms |
25948 KB |
Output is correct |
59 |
Correct |
17 ms |
25948 KB |
Output is correct |
60 |
Correct |
23 ms |
26204 KB |
Output is correct |
61 |
Correct |
19 ms |
26204 KB |
Output is correct |
62 |
Correct |
44 ms |
26972 KB |
Output is correct |
63 |
Correct |
40 ms |
27092 KB |
Output is correct |
64 |
Correct |
42 ms |
26968 KB |
Output is correct |
65 |
Correct |
243 ms |
27348 KB |
Output is correct |
66 |
Correct |
702 ms |
27860 KB |
Output is correct |
67 |
Correct |
701 ms |
27860 KB |
Output is correct |