#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
const int INF = 1e9;
const int BLK = 5000;
int n, m;
vector<int> b, p;
vector<vector<int>> v;
int f(int x, int c, int y) {
return x + c * y;
}
void addDoge(int i, int pos, int d, vector<vector<int>> &dist, priority_queue<tuple<int, int, int>> &q) {
if (p[i] < BLK) {
dist[pos][p[i]] = min(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) {
dist[l][0] = min(dist[l][0], -d + x);
q.push(make_tuple(d - x, l, 0));
}
if (r < n) {
dist[r][0] = min(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 (pos + power < n) {
dist[pos + power][power] = min(dist[pos + power][power], -d + 1);
q.push(make_tuple(d - 1, pos + power, power));
}
if (pos - power >= 0) {
dist[pos - power][power] = min(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 |
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 |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 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 |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2396 KB |
Output is correct |
12 |
Correct |
29 ms |
2652 KB |
Output is correct |
13 |
Correct |
3 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
412 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2396 KB |
Output is correct |
12 |
Correct |
31 ms |
2652 KB |
Output is correct |
13 |
Correct |
3 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
3 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
9 ms |
14940 KB |
Output is correct |
18 |
Correct |
14 ms |
34652 KB |
Output is correct |
19 |
Correct |
16 ms |
39616 KB |
Output is correct |
20 |
Correct |
309 ms |
40080 KB |
Output is correct |
21 |
Correct |
4 ms |
8028 KB |
Output is correct |
22 |
Correct |
13 ms |
35420 KB |
Output is correct |
23 |
Correct |
14 ms |
31768 KB |
Output is correct |
24 |
Correct |
17 ms |
37852 KB |
Output is correct |
25 |
Correct |
17 ms |
39756 KB |
Output is correct |
26 |
Correct |
487 ms |
39836 KB |
Output is correct |
27 |
Correct |
506 ms |
40028 KB |
Output is correct |
28 |
Correct |
17 ms |
39892 KB |
Output is correct |
29 |
Correct |
26 ms |
39768 KB |
Output is correct |
30 |
Correct |
18 ms |
39516 KB |
Output is correct |
31 |
Correct |
21 ms |
39772 KB |
Output is correct |
32 |
Correct |
20 ms |
39516 KB |
Output is correct |
33 |
Correct |
43 ms |
39772 KB |
Output is correct |
34 |
Correct |
41 ms |
39772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2392 KB |
Output is correct |
12 |
Correct |
30 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2392 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
9 ms |
14936 KB |
Output is correct |
18 |
Correct |
13 ms |
34648 KB |
Output is correct |
19 |
Correct |
15 ms |
39512 KB |
Output is correct |
20 |
Correct |
322 ms |
39772 KB |
Output is correct |
21 |
Correct |
3 ms |
8024 KB |
Output is correct |
22 |
Correct |
16 ms |
35448 KB |
Output is correct |
23 |
Correct |
13 ms |
31836 KB |
Output is correct |
24 |
Correct |
18 ms |
37724 KB |
Output is correct |
25 |
Correct |
18 ms |
39640 KB |
Output is correct |
26 |
Correct |
485 ms |
39768 KB |
Output is correct |
27 |
Correct |
545 ms |
39896 KB |
Output is correct |
28 |
Correct |
17 ms |
39772 KB |
Output is correct |
29 |
Correct |
27 ms |
39772 KB |
Output is correct |
30 |
Correct |
17 ms |
39516 KB |
Output is correct |
31 |
Correct |
21 ms |
39772 KB |
Output is correct |
32 |
Correct |
18 ms |
39516 KB |
Output is correct |
33 |
Correct |
39 ms |
39772 KB |
Output is correct |
34 |
Correct |
39 ms |
40160 KB |
Output is correct |
35 |
Correct |
43 ms |
29452 KB |
Output is correct |
36 |
Correct |
12 ms |
21852 KB |
Output is correct |
37 |
Correct |
66 ms |
39728 KB |
Output is correct |
38 |
Correct |
65 ms |
41052 KB |
Output is correct |
39 |
Correct |
66 ms |
41100 KB |
Output is correct |
40 |
Correct |
67 ms |
41052 KB |
Output is correct |
41 |
Correct |
64 ms |
41060 KB |
Output is correct |
42 |
Execution timed out |
1050 ms |
40800 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
576 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2500 KB |
Output is correct |
12 |
Correct |
30 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2392 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
16 |
Correct |
3 ms |
4184 KB |
Output is correct |
17 |
Correct |
9 ms |
14940 KB |
Output is correct |
18 |
Correct |
14 ms |
34652 KB |
Output is correct |
19 |
Correct |
15 ms |
39516 KB |
Output is correct |
20 |
Correct |
310 ms |
39832 KB |
Output is correct |
21 |
Correct |
3 ms |
8028 KB |
Output is correct |
22 |
Correct |
14 ms |
35444 KB |
Output is correct |
23 |
Correct |
14 ms |
31836 KB |
Output is correct |
24 |
Correct |
17 ms |
37720 KB |
Output is correct |
25 |
Correct |
17 ms |
39772 KB |
Output is correct |
26 |
Correct |
478 ms |
39768 KB |
Output is correct |
27 |
Correct |
505 ms |
40028 KB |
Output is correct |
28 |
Correct |
18 ms |
39768 KB |
Output is correct |
29 |
Correct |
28 ms |
39772 KB |
Output is correct |
30 |
Correct |
18 ms |
39516 KB |
Output is correct |
31 |
Correct |
23 ms |
39760 KB |
Output is correct |
32 |
Correct |
23 ms |
39516 KB |
Output is correct |
33 |
Correct |
42 ms |
39896 KB |
Output is correct |
34 |
Correct |
42 ms |
39772 KB |
Output is correct |
35 |
Correct |
46 ms |
29456 KB |
Output is correct |
36 |
Correct |
14 ms |
21852 KB |
Output is correct |
37 |
Correct |
68 ms |
39600 KB |
Output is correct |
38 |
Correct |
69 ms |
41052 KB |
Output is correct |
39 |
Correct |
66 ms |
41060 KB |
Output is correct |
40 |
Correct |
64 ms |
41044 KB |
Output is correct |
41 |
Correct |
67 ms |
41100 KB |
Output is correct |
42 |
Execution timed out |
1062 ms |
40804 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |