#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
const int INF = 1e9;
struct X {
int d, j, b, type;
X(int d, int j, int b, int type) : d {d}, j {j}, b {b}, type {type} {}
};
const int N = 3e4, K = 173;
int dist[N][K], n, m, s, t, j;
vector<int> jumps[N];
queue<X> q;
void solve() {
cin >> n >> m;
for(int i = 0; i < n; ++i)
for(int j = 0; j * j <= n; ++j)
dist[i][j] = -1;
for(int i = 0; i < m; ++i) {
int b, p;
cin >> b >> p;
if(i == 0) s = b, j = p;
else if(i == 1) t = b;
jumps[b].push_back(p);
}
for(int i = 0; i < n; ++i) {
auto& v = jumps[i];
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
if(j * j <= n) {
dist[s][j] = 0;
q.emplace(0, j, s, 0);
} else {
q.emplace(0, j, s, -1);
q.emplace(0, j, s, 1);
}
while(!q.empty()) {
auto [d, j, b, type] = q.front();
if(b == t) {
cout << d << '\n';
return;
}
q.pop();
if(type == -1) {
if(b - j >= 0) q.emplace(d + 1, j, b - j, -1);
} else if(type == 1) {
if(b + j < n) q.emplace(d + 1, j, b + j, 1);
} else {
if(b - j >= 0 && dist[b - j][j] == -1) {
dist[b - j][j] = 1 + d;
q.emplace(d + 1, j, b - j, 0);
}
if(b + j < n && dist[b + j][j] == -1) {
dist[b + j][j] = 1 + d;
q.emplace(d + 1, j, b + j, 0);
}
}
while(!jumps[b].empty()) {
int p = jumps[b].back();
jumps[b].pop_back();
if(p * p <= n) {
if(b - p >= 0 && dist[b - p][p] == -1) {
dist[b - p][p] = d + 1;
q.emplace(d + 1, p, b - p, 0);
}
if(b + p < n && dist[b + p][p] == -1) {
dist[b + p][p] = d + 1;
q.emplace(d + 1, p, b + p, 0);
}
} else {
if(b - p >= 0) q.emplace(d + 1, p, b - p, -1);
if(b + p < n) q.emplace(d + 1, p, b + p, 1);
}
}
}
cout << "-1\n";
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
1028 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
1032 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
1092 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1036 KB |
Output is correct |
13 |
Correct |
1 ms |
1032 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
1028 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1028 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1504 KB |
Output is correct |
18 |
Correct |
1 ms |
2184 KB |
Output is correct |
19 |
Correct |
1 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2380 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
3 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
1 ms |
2324 KB |
Output is correct |
27 |
Correct |
2 ms |
2380 KB |
Output is correct |
28 |
Correct |
3 ms |
2380 KB |
Output is correct |
29 |
Correct |
2 ms |
2380 KB |
Output is correct |
30 |
Correct |
2 ms |
2380 KB |
Output is correct |
31 |
Correct |
2 ms |
2380 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
3 ms |
2448 KB |
Output is correct |
34 |
Correct |
2 ms |
2380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
1028 KB |
Output is correct |
3 |
Correct |
1 ms |
1028 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
1032 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1036 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1176 KB |
Output is correct |
17 |
Correct |
2 ms |
1552 KB |
Output is correct |
18 |
Correct |
2 ms |
2124 KB |
Output is correct |
19 |
Correct |
1 ms |
2308 KB |
Output is correct |
20 |
Correct |
2 ms |
2440 KB |
Output is correct |
21 |
Correct |
1 ms |
1292 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2076 KB |
Output is correct |
24 |
Correct |
2 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
2 ms |
2324 KB |
Output is correct |
27 |
Correct |
2 ms |
2380 KB |
Output is correct |
28 |
Correct |
2 ms |
2380 KB |
Output is correct |
29 |
Correct |
3 ms |
2380 KB |
Output is correct |
30 |
Correct |
2 ms |
2380 KB |
Output is correct |
31 |
Correct |
2 ms |
2380 KB |
Output is correct |
32 |
Correct |
2 ms |
2308 KB |
Output is correct |
33 |
Correct |
3 ms |
2380 KB |
Output is correct |
34 |
Correct |
3 ms |
2380 KB |
Output is correct |
35 |
Correct |
6 ms |
2580 KB |
Output is correct |
36 |
Correct |
2 ms |
1740 KB |
Output is correct |
37 |
Correct |
5 ms |
2892 KB |
Output is correct |
38 |
Correct |
7 ms |
3140 KB |
Output is correct |
39 |
Correct |
7 ms |
2764 KB |
Output is correct |
40 |
Correct |
7 ms |
2892 KB |
Output is correct |
41 |
Correct |
7 ms |
2824 KB |
Output is correct |
42 |
Correct |
5 ms |
2780 KB |
Output is correct |
43 |
Correct |
5 ms |
2772 KB |
Output is correct |
44 |
Correct |
5 ms |
2704 KB |
Output is correct |
45 |
Correct |
11 ms |
3260 KB |
Output is correct |
46 |
Correct |
10 ms |
3304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1020 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1036 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1484 KB |
Output is correct |
18 |
Correct |
1 ms |
2124 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2380 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
2 ms |
2312 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
2 ms |
2380 KB |
Output is correct |
27 |
Correct |
2 ms |
2320 KB |
Output is correct |
28 |
Correct |
2 ms |
2380 KB |
Output is correct |
29 |
Correct |
3 ms |
2340 KB |
Output is correct |
30 |
Correct |
2 ms |
2304 KB |
Output is correct |
31 |
Correct |
2 ms |
2380 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
3 ms |
2380 KB |
Output is correct |
34 |
Correct |
2 ms |
2444 KB |
Output is correct |
35 |
Correct |
7 ms |
2568 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
5 ms |
2952 KB |
Output is correct |
38 |
Correct |
7 ms |
3148 KB |
Output is correct |
39 |
Correct |
7 ms |
2764 KB |
Output is correct |
40 |
Correct |
7 ms |
2820 KB |
Output is correct |
41 |
Correct |
7 ms |
2892 KB |
Output is correct |
42 |
Correct |
6 ms |
2712 KB |
Output is correct |
43 |
Correct |
5 ms |
2756 KB |
Output is correct |
44 |
Correct |
5 ms |
2772 KB |
Output is correct |
45 |
Correct |
12 ms |
3292 KB |
Output is correct |
46 |
Correct |
8 ms |
3216 KB |
Output is correct |
47 |
Correct |
11 ms |
10220 KB |
Output is correct |
48 |
Correct |
15 ms |
17356 KB |
Output is correct |
49 |
Correct |
15 ms |
18764 KB |
Output is correct |
50 |
Correct |
15 ms |
20372 KB |
Output is correct |
51 |
Correct |
24 ms |
22304 KB |
Output is correct |
52 |
Correct |
26 ms |
22428 KB |
Output is correct |
53 |
Correct |
20 ms |
22160 KB |
Output is correct |
54 |
Correct |
13 ms |
21232 KB |
Output is correct |
55 |
Correct |
14 ms |
21244 KB |
Output is correct |
56 |
Correct |
20 ms |
22492 KB |
Output is correct |
57 |
Correct |
13 ms |
21384 KB |
Output is correct |
58 |
Correct |
18 ms |
21580 KB |
Output is correct |
59 |
Correct |
19 ms |
21748 KB |
Output is correct |
60 |
Correct |
19 ms |
21732 KB |
Output is correct |
61 |
Correct |
21 ms |
21764 KB |
Output is correct |
62 |
Correct |
42 ms |
22568 KB |
Output is correct |
63 |
Correct |
42 ms |
22004 KB |
Output is correct |
64 |
Correct |
47 ms |
22084 KB |
Output is correct |
65 |
Correct |
83 ms |
22156 KB |
Output is correct |
66 |
Correct |
246 ms |
22400 KB |
Output is correct |
67 |
Correct |
79 ms |
22476 KB |
Output is correct |