#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()
const int mxn = 30010;
int n, m;
unordered_map<int, ll> best[mxn];
vector<int> val[mxn];
int spot[mxn], jump[mxn];
int dx[] = {-1, 1};
void solve() {
priority_queue<pair<ll, pair<int, int>>> pq;
pq.push({0, {spot[0], jump[0]}});
best[spot[0]][jump[0]] = 0;
while (pq.size()) {
ll dist = -pq.top().first, pos = pq.top().second.first, pwr = pq.top().second.second;
pq.pop();
for (auto x : val[pos]) {
if (!best[pos].count(jump[x])) best[pos][jump[x]] = 1e18;
if (dist < best[pos][jump[x]]) {
best[pos][jump[x]] = dist;
pq.push({-dist, {pos, jump[x]}});
}
}
for (int d = 0; d < 2; d++) {
int newPos = pos + pwr * dx[d];
if (0 <= newPos && newPos < n) {
if (!best[newPos].count(pwr)) best[newPos][pwr] = 1e18;
if (dist + 1 < best[newPos][pwr]) {
best[newPos][pwr] = dist + 1;
pq.push({-best[newPos][pwr], {newPos, pwr}});
}
}
}
}
ll ans = 1e18;
for (auto x : best[spot[1]]) ans = min(ans, x.second);
cout << (ans != 1e18 ? ans : -1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> spot[i] >> jump[i];
val[spot[i]].push_back(i);
}
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2740 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2904 KB |
Output is correct |
11 |
Correct |
4 ms |
3084 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
3164 KB |
Output is correct |
15 |
Correct |
4 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
4 ms |
2904 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
3164 KB |
Output is correct |
15 |
Correct |
4 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
6 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
2 ms |
3164 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
3 ms |
3284 KB |
Output is correct |
24 |
Correct |
5 ms |
3420 KB |
Output is correct |
25 |
Correct |
4 ms |
3160 KB |
Output is correct |
26 |
Correct |
3 ms |
3164 KB |
Output is correct |
27 |
Correct |
7 ms |
3164 KB |
Output is correct |
28 |
Correct |
6 ms |
3932 KB |
Output is correct |
29 |
Correct |
18 ms |
5652 KB |
Output is correct |
30 |
Correct |
5 ms |
3420 KB |
Output is correct |
31 |
Correct |
9 ms |
4188 KB |
Output is correct |
32 |
Correct |
5 ms |
3768 KB |
Output is correct |
33 |
Correct |
28 ms |
8284 KB |
Output is correct |
34 |
Correct |
28 ms |
8364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
4 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
3164 KB |
Output is correct |
15 |
Correct |
4 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
2740 KB |
Output is correct |
17 |
Correct |
6 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
2904 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
3164 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
5 ms |
3164 KB |
Output is correct |
24 |
Correct |
5 ms |
3420 KB |
Output is correct |
25 |
Correct |
4 ms |
3416 KB |
Output is correct |
26 |
Correct |
3 ms |
3164 KB |
Output is correct |
27 |
Correct |
7 ms |
3220 KB |
Output is correct |
28 |
Correct |
5 ms |
3932 KB |
Output is correct |
29 |
Correct |
14 ms |
5468 KB |
Output is correct |
30 |
Correct |
4 ms |
3420 KB |
Output is correct |
31 |
Correct |
7 ms |
4188 KB |
Output is correct |
32 |
Correct |
5 ms |
3660 KB |
Output is correct |
33 |
Correct |
28 ms |
8284 KB |
Output is correct |
34 |
Correct |
28 ms |
8280 KB |
Output is correct |
35 |
Correct |
80 ms |
7884 KB |
Output is correct |
36 |
Correct |
10 ms |
3676 KB |
Output is correct |
37 |
Correct |
103 ms |
10736 KB |
Output is correct |
38 |
Correct |
115 ms |
10700 KB |
Output is correct |
39 |
Correct |
117 ms |
10496 KB |
Output is correct |
40 |
Correct |
129 ms |
10952 KB |
Output is correct |
41 |
Correct |
118 ms |
10444 KB |
Output is correct |
42 |
Correct |
7 ms |
3420 KB |
Output is correct |
43 |
Correct |
72 ms |
3500 KB |
Output is correct |
44 |
Correct |
5 ms |
3420 KB |
Output is correct |
45 |
Correct |
215 ms |
22740 KB |
Output is correct |
46 |
Correct |
212 ms |
22676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2712 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
5 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
5 ms |
3164 KB |
Output is correct |
15 |
Correct |
4 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
6 ms |
3420 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
3224 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
3416 KB |
Output is correct |
24 |
Correct |
4 ms |
3420 KB |
Output is correct |
25 |
Correct |
3 ms |
3416 KB |
Output is correct |
26 |
Correct |
2 ms |
3164 KB |
Output is correct |
27 |
Correct |
6 ms |
3164 KB |
Output is correct |
28 |
Correct |
6 ms |
3932 KB |
Output is correct |
29 |
Correct |
17 ms |
5608 KB |
Output is correct |
30 |
Correct |
4 ms |
3416 KB |
Output is correct |
31 |
Correct |
8 ms |
4188 KB |
Output is correct |
32 |
Correct |
5 ms |
3672 KB |
Output is correct |
33 |
Correct |
27 ms |
8272 KB |
Output is correct |
34 |
Correct |
28 ms |
8220 KB |
Output is correct |
35 |
Correct |
84 ms |
7884 KB |
Output is correct |
36 |
Correct |
8 ms |
3676 KB |
Output is correct |
37 |
Correct |
108 ms |
10884 KB |
Output is correct |
38 |
Correct |
121 ms |
10636 KB |
Output is correct |
39 |
Correct |
116 ms |
10688 KB |
Output is correct |
40 |
Correct |
121 ms |
10496 KB |
Output is correct |
41 |
Correct |
122 ms |
10608 KB |
Output is correct |
42 |
Correct |
7 ms |
3420 KB |
Output is correct |
43 |
Correct |
71 ms |
3488 KB |
Output is correct |
44 |
Correct |
5 ms |
3420 KB |
Output is correct |
45 |
Correct |
227 ms |
23008 KB |
Output is correct |
46 |
Correct |
232 ms |
22768 KB |
Output is correct |
47 |
Correct |
557 ms |
36372 KB |
Output is correct |
48 |
Correct |
5 ms |
3420 KB |
Output is correct |
49 |
Correct |
5 ms |
3420 KB |
Output is correct |
50 |
Correct |
4 ms |
3164 KB |
Output is correct |
51 |
Correct |
142 ms |
17624 KB |
Output is correct |
52 |
Correct |
159 ms |
18888 KB |
Output is correct |
53 |
Correct |
25 ms |
9564 KB |
Output is correct |
54 |
Correct |
5 ms |
4952 KB |
Output is correct |
55 |
Correct |
7 ms |
5724 KB |
Output is correct |
56 |
Correct |
15 ms |
8132 KB |
Output is correct |
57 |
Correct |
142 ms |
23788 KB |
Output is correct |
58 |
Correct |
21 ms |
8540 KB |
Output is correct |
59 |
Correct |
22 ms |
9460 KB |
Output is correct |
60 |
Correct |
27 ms |
10324 KB |
Output is correct |
61 |
Correct |
721 ms |
9852 KB |
Output is correct |
62 |
Correct |
188 ms |
26216 KB |
Output is correct |
63 |
Execution timed out |
1086 ms |
83720 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |