#include <bits/stdc++.h>
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<int> &dist, priority_queue<pair<int, int>> &q, bool dir) {
int c = (dir ? 1 : -1);
for (int pos_nxt = pos + c * p[i], x = 1; (dir ? pos_nxt < n : pos_nxt >= 0); pos_nxt += c * p[i], x++) {
if (-d + x < dist[pos_nxt]) {
dist[pos_nxt] = -d + x;
q.push({d - x, pos_nxt});
}
}
}
int solve() {
vector<int> dist(n, INF);
priority_queue<pair<int, int>> q;
dist[b[0]] = 0;
q.push({0, b[0]});
while (not q.empty()) {
auto [d, pos] = q.top();
q.pop();
if (dist[pos] != -d) continue;
while (not v[pos].empty()) {
addDoge(v[pos].back(), pos, d, dist, q, 0);
addDoge(v[pos].back(), pos, d, dist, q, 1);
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]]);
}
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);
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
v[b[i]].push_back(i);
}
cout << solve() << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
484 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
544 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
4 ms |
552 KB |
Output is correct |
27 |
Correct |
4 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
5 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
4 ms |
856 KB |
Output is correct |
38 |
Correct |
6 ms |
1116 KB |
Output is correct |
39 |
Correct |
6 ms |
1116 KB |
Output is correct |
40 |
Correct |
6 ms |
1116 KB |
Output is correct |
41 |
Correct |
6 ms |
1112 KB |
Output is correct |
42 |
Correct |
59 ms |
860 KB |
Output is correct |
43 |
Correct |
60 ms |
860 KB |
Output is correct |
44 |
Correct |
57 ms |
860 KB |
Output is correct |
45 |
Correct |
5 ms |
856 KB |
Output is correct |
46 |
Correct |
5 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
624 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
5 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
6 ms |
1116 KB |
Output is correct |
38 |
Correct |
6 ms |
1116 KB |
Output is correct |
39 |
Correct |
6 ms |
1116 KB |
Output is correct |
40 |
Correct |
8 ms |
1116 KB |
Output is correct |
41 |
Correct |
6 ms |
1116 KB |
Output is correct |
42 |
Correct |
63 ms |
856 KB |
Output is correct |
43 |
Correct |
59 ms |
856 KB |
Output is correct |
44 |
Correct |
56 ms |
856 KB |
Output is correct |
45 |
Correct |
6 ms |
860 KB |
Output is correct |
46 |
Correct |
5 ms |
856 KB |
Output is correct |
47 |
Correct |
13 ms |
1752 KB |
Output is correct |
48 |
Correct |
5 ms |
1628 KB |
Output is correct |
49 |
Correct |
5 ms |
1628 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
20 ms |
3260 KB |
Output is correct |
52 |
Correct |
22 ms |
2880 KB |
Output is correct |
53 |
Correct |
11 ms |
2772 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
859 ms |
2776 KB |
Output is correct |
57 |
Correct |
40 ms |
3532 KB |
Output is correct |
58 |
Correct |
906 ms |
2128 KB |
Output is correct |
59 |
Correct |
805 ms |
2132 KB |
Output is correct |
60 |
Correct |
869 ms |
2084 KB |
Output is correct |
61 |
Correct |
724 ms |
2140 KB |
Output is correct |
62 |
Correct |
8 ms |
2652 KB |
Output is correct |
63 |
Correct |
19 ms |
3028 KB |
Output is correct |
64 |
Correct |
19 ms |
3024 KB |
Output is correct |
65 |
Correct |
22 ms |
3028 KB |
Output is correct |
66 |
Correct |
28 ms |
3188 KB |
Output is correct |
67 |
Correct |
30 ms |
3028 KB |
Output is correct |