#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 {
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()) {
addDoge(v[pos].back(), pos, d, dist, q);
v[pos].pop_back();
}
if (power and 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 (power and 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 |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
424 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
1 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 |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 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 |
348 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 |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
2136 KB |
Output is correct |
20 |
Correct |
7 ms |
2140 KB |
Output is correct |
21 |
Correct |
0 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 |
2 ms |
1880 KB |
Output is correct |
25 |
Correct |
2 ms |
2140 KB |
Output is correct |
26 |
Correct |
7 ms |
2140 KB |
Output is correct |
27 |
Correct |
7 ms |
2140 KB |
Output is correct |
28 |
Correct |
2 ms |
2136 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 |
14 ms |
2140 KB |
Output is correct |
34 |
Correct |
12 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
376 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 |
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 |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 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 |
1880 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
7 ms |
2136 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
1880 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
2136 KB |
Output is correct |
26 |
Correct |
8 ms |
2292 KB |
Output is correct |
27 |
Correct |
6 ms |
2136 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 |
2392 KB |
Output is correct |
33 |
Correct |
12 ms |
2140 KB |
Output is correct |
34 |
Correct |
12 ms |
2140 KB |
Output is correct |
35 |
Correct |
11 ms |
2140 KB |
Output is correct |
36 |
Correct |
3 ms |
1368 KB |
Output is correct |
37 |
Correct |
17 ms |
2396 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 |
2816 KB |
Output is correct |
41 |
Correct |
16 ms |
2652 KB |
Output is correct |
42 |
Correct |
94 ms |
2396 KB |
Output is correct |
43 |
Correct |
86 ms |
2396 KB |
Output is correct |
44 |
Correct |
96 ms |
2644 KB |
Output is correct |
45 |
Correct |
49 ms |
3100 KB |
Output is correct |
46 |
Correct |
54 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 |
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 |
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 |
348 KB |
Output is correct |
11 |
Correct |
2 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 |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 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 |
7 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 |
1624 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
2136 KB |
Output is correct |
26 |
Correct |
9 ms |
2140 KB |
Output is correct |
27 |
Correct |
6 ms |
2140 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
6 ms |
2204 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 |
2136 KB |
Output is correct |
34 |
Correct |
12 ms |
2140 KB |
Output is correct |
35 |
Correct |
11 ms |
2140 KB |
Output is correct |
36 |
Correct |
3 ms |
1372 KB |
Output is correct |
37 |
Correct |
17 ms |
2604 KB |
Output is correct |
38 |
Correct |
15 ms |
2652 KB |
Output is correct |
39 |
Correct |
17 ms |
2908 KB |
Output is correct |
40 |
Correct |
16 ms |
2652 KB |
Output is correct |
41 |
Correct |
15 ms |
2652 KB |
Output is correct |
42 |
Correct |
94 ms |
2400 KB |
Output is correct |
43 |
Correct |
88 ms |
2392 KB |
Output is correct |
44 |
Correct |
96 ms |
2396 KB |
Output is correct |
45 |
Correct |
54 ms |
3100 KB |
Output is correct |
46 |
Correct |
49 ms |
3096 KB |
Output is correct |
47 |
Correct |
68 ms |
11800 KB |
Output is correct |
48 |
Correct |
13 ms |
20572 KB |
Output is correct |
49 |
Correct |
14 ms |
22540 KB |
Output is correct |
50 |
Correct |
13 ms |
24404 KB |
Output is correct |
51 |
Correct |
48 ms |
27168 KB |
Output is correct |
52 |
Correct |
64 ms |
27164 KB |
Output is correct |
53 |
Correct |
24 ms |
27164 KB |
Output is correct |
54 |
Correct |
12 ms |
25600 KB |
Output is correct |
55 |
Correct |
12 ms |
25692 KB |
Output is correct |
56 |
Execution timed out |
1096 ms |
27456 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |