#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld double
#define ll long long
mt19937 rnd(51);
const int N = 30010, K = 50, INF = 1e9, M = 1e5 + 10;
int d[N][K];
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
d[i][j] = INF;
}
}
int n, m;
cin >> n >> m;
vector<int> b(m), p(m), ans(n, INF);
vector<vector<int>> need(n);
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
need[b[i]].pb(abs(p[i]));
}
ans[b[0]] = 0;
set<array<int, 3>> st;
st.insert({0, b[0], -1});
while (st.size() > 0) {
auto arr = *st.begin();
st.erase(arr);
int v = arr[1], j = arr[2];
if (j == -1) {
for (auto to : need[v]) {
if (to < K) {
if (d[v][to] > ans[v]) {
st.erase({d[v][to], v, to});
d[v][to] = ans[v];
st.insert({d[v][to], v, to});
}
} else {
for (int k = v, add = 0; k < n; k += to, add++) {
if (ans[k] > ans[v] + add) {
st.erase({ans[k], k, -1});
ans[k] = ans[v] + add;
st.insert({ans[k], k, -1});
}
}
for (int k = v, add = 0; k >= 0; k -= to, add++) {
if (ans[k] > ans[v] + add) {
st.erase({ans[k], k, -1});
ans[k] = ans[v] + add;
st.insert({ans[k], k, -1});
}
}
}
}
} else {
if (d[v][j] < ans[v]) {
st.erase({ans[v], v, -1});
ans[v] = d[v][j];
st.insert({ans[v], v, -1});
}
if (v + j < n && d[v + j][j] > d[v][j] + 1) {
st.erase({d[v + j][j], v + j, j});
d[v + j][j] = d[v][j] + 1;
st.insert({d[v + j][j], v + j, j});
}
if (v - j >= 0 && d[v - j][j] > d[v][j] + 1) {
st.erase({d[v - j][j], v - j, j});
d[v - j][j] = d[v][j] + 1;
st.insert({d[v - j][j], v - j, j});
}
}
}
cout << (ans[b[1]] == INF ? -1 : ans[b[1]]) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
4 ms |
6100 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
5 ms |
6100 KB |
Output is correct |
6 |
Correct |
4 ms |
6100 KB |
Output is correct |
7 |
Correct |
4 ms |
6100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
4 ms |
6100 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
5 ms |
6100 KB |
Output is correct |
5 |
Correct |
4 ms |
6100 KB |
Output is correct |
6 |
Correct |
4 ms |
6096 KB |
Output is correct |
7 |
Correct |
4 ms |
6152 KB |
Output is correct |
8 |
Correct |
4 ms |
6100 KB |
Output is correct |
9 |
Correct |
4 ms |
6100 KB |
Output is correct |
10 |
Correct |
4 ms |
6100 KB |
Output is correct |
11 |
Correct |
7 ms |
6228 KB |
Output is correct |
12 |
Correct |
4 ms |
6228 KB |
Output is correct |
13 |
Correct |
5 ms |
6228 KB |
Output is correct |
14 |
Correct |
6 ms |
6228 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
4 ms |
6100 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6132 KB |
Output is correct |
5 |
Correct |
4 ms |
6100 KB |
Output is correct |
6 |
Correct |
4 ms |
6100 KB |
Output is correct |
7 |
Correct |
4 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6100 KB |
Output is correct |
9 |
Correct |
4 ms |
6100 KB |
Output is correct |
10 |
Correct |
5 ms |
6228 KB |
Output is correct |
11 |
Correct |
4 ms |
6228 KB |
Output is correct |
12 |
Correct |
4 ms |
6228 KB |
Output is correct |
13 |
Correct |
5 ms |
6228 KB |
Output is correct |
14 |
Correct |
6 ms |
6304 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
4 ms |
6100 KB |
Output is correct |
17 |
Correct |
7 ms |
6280 KB |
Output is correct |
18 |
Correct |
4 ms |
6228 KB |
Output is correct |
19 |
Correct |
4 ms |
6228 KB |
Output is correct |
20 |
Correct |
5 ms |
6228 KB |
Output is correct |
21 |
Correct |
4 ms |
6228 KB |
Output is correct |
22 |
Correct |
4 ms |
6228 KB |
Output is correct |
23 |
Correct |
5 ms |
6236 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
5 ms |
6228 KB |
Output is correct |
26 |
Correct |
5 ms |
6228 KB |
Output is correct |
27 |
Correct |
5 ms |
6228 KB |
Output is correct |
28 |
Correct |
6 ms |
6228 KB |
Output is correct |
29 |
Correct |
20 ms |
6228 KB |
Output is correct |
30 |
Correct |
8 ms |
6228 KB |
Output is correct |
31 |
Correct |
12 ms |
6228 KB |
Output is correct |
32 |
Correct |
10 ms |
6228 KB |
Output is correct |
33 |
Correct |
30 ms |
6356 KB |
Output is correct |
34 |
Correct |
31 ms |
6448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6100 KB |
Output is correct |
2 |
Correct |
4 ms |
6100 KB |
Output is correct |
3 |
Correct |
6 ms |
6100 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
4 ms |
6100 KB |
Output is correct |
6 |
Correct |
5 ms |
6100 KB |
Output is correct |
7 |
Correct |
5 ms |
6144 KB |
Output is correct |
8 |
Correct |
4 ms |
6100 KB |
Output is correct |
9 |
Correct |
4 ms |
6100 KB |
Output is correct |
10 |
Correct |
4 ms |
6100 KB |
Output is correct |
11 |
Correct |
5 ms |
6228 KB |
Output is correct |
12 |
Correct |
4 ms |
6228 KB |
Output is correct |
13 |
Correct |
4 ms |
6228 KB |
Output is correct |
14 |
Correct |
7 ms |
6268 KB |
Output is correct |
15 |
Correct |
6 ms |
6228 KB |
Output is correct |
16 |
Correct |
5 ms |
6100 KB |
Output is correct |
17 |
Correct |
7 ms |
6292 KB |
Output is correct |
18 |
Correct |
4 ms |
6244 KB |
Output is correct |
19 |
Correct |
4 ms |
6228 KB |
Output is correct |
20 |
Correct |
4 ms |
6228 KB |
Output is correct |
21 |
Correct |
4 ms |
6160 KB |
Output is correct |
22 |
Correct |
5 ms |
6228 KB |
Output is correct |
23 |
Correct |
5 ms |
6228 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
5 ms |
6228 KB |
Output is correct |
26 |
Correct |
5 ms |
6228 KB |
Output is correct |
27 |
Correct |
4 ms |
6228 KB |
Output is correct |
28 |
Correct |
6 ms |
6228 KB |
Output is correct |
29 |
Correct |
21 ms |
6308 KB |
Output is correct |
30 |
Correct |
8 ms |
6228 KB |
Output is correct |
31 |
Correct |
13 ms |
6228 KB |
Output is correct |
32 |
Correct |
10 ms |
6316 KB |
Output is correct |
33 |
Correct |
29 ms |
6356 KB |
Output is correct |
34 |
Correct |
35 ms |
6356 KB |
Output is correct |
35 |
Correct |
18 ms |
6708 KB |
Output is correct |
36 |
Correct |
7 ms |
6228 KB |
Output is correct |
37 |
Correct |
27 ms |
6676 KB |
Output is correct |
38 |
Correct |
23 ms |
6740 KB |
Output is correct |
39 |
Correct |
24 ms |
6828 KB |
Output is correct |
40 |
Correct |
29 ms |
6740 KB |
Output is correct |
41 |
Correct |
34 ms |
6804 KB |
Output is correct |
42 |
Correct |
9 ms |
6740 KB |
Output is correct |
43 |
Correct |
9 ms |
6740 KB |
Output is correct |
44 |
Correct |
9 ms |
6656 KB |
Output is correct |
45 |
Correct |
34 ms |
6852 KB |
Output is correct |
46 |
Correct |
31 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
4 ms |
6100 KB |
Output is correct |
6 |
Correct |
4 ms |
6100 KB |
Output is correct |
7 |
Correct |
4 ms |
6076 KB |
Output is correct |
8 |
Correct |
5 ms |
6100 KB |
Output is correct |
9 |
Correct |
4 ms |
6100 KB |
Output is correct |
10 |
Correct |
4 ms |
6224 KB |
Output is correct |
11 |
Correct |
5 ms |
6228 KB |
Output is correct |
12 |
Correct |
5 ms |
6228 KB |
Output is correct |
13 |
Correct |
4 ms |
6228 KB |
Output is correct |
14 |
Correct |
6 ms |
6256 KB |
Output is correct |
15 |
Correct |
6 ms |
6356 KB |
Output is correct |
16 |
Correct |
4 ms |
6100 KB |
Output is correct |
17 |
Correct |
7 ms |
6232 KB |
Output is correct |
18 |
Correct |
4 ms |
6228 KB |
Output is correct |
19 |
Correct |
4 ms |
6228 KB |
Output is correct |
20 |
Correct |
5 ms |
6228 KB |
Output is correct |
21 |
Correct |
4 ms |
6228 KB |
Output is correct |
22 |
Correct |
4 ms |
6228 KB |
Output is correct |
23 |
Correct |
6 ms |
6228 KB |
Output is correct |
24 |
Correct |
7 ms |
6228 KB |
Output is correct |
25 |
Correct |
5 ms |
6228 KB |
Output is correct |
26 |
Correct |
5 ms |
6228 KB |
Output is correct |
27 |
Correct |
5 ms |
6228 KB |
Output is correct |
28 |
Correct |
5 ms |
6228 KB |
Output is correct |
29 |
Correct |
18 ms |
6308 KB |
Output is correct |
30 |
Correct |
7 ms |
6228 KB |
Output is correct |
31 |
Correct |
11 ms |
6300 KB |
Output is correct |
32 |
Correct |
10 ms |
6228 KB |
Output is correct |
33 |
Correct |
28 ms |
6440 KB |
Output is correct |
34 |
Correct |
28 ms |
6456 KB |
Output is correct |
35 |
Correct |
18 ms |
6612 KB |
Output is correct |
36 |
Correct |
7 ms |
6352 KB |
Output is correct |
37 |
Correct |
25 ms |
6624 KB |
Output is correct |
38 |
Correct |
24 ms |
6832 KB |
Output is correct |
39 |
Correct |
24 ms |
6812 KB |
Output is correct |
40 |
Correct |
25 ms |
6740 KB |
Output is correct |
41 |
Correct |
23 ms |
6740 KB |
Output is correct |
42 |
Correct |
8 ms |
6612 KB |
Output is correct |
43 |
Correct |
8 ms |
6740 KB |
Output is correct |
44 |
Correct |
11 ms |
6612 KB |
Output is correct |
45 |
Correct |
34 ms |
6860 KB |
Output is correct |
46 |
Correct |
32 ms |
6852 KB |
Output is correct |
47 |
Correct |
93 ms |
7764 KB |
Output is correct |
48 |
Correct |
9 ms |
7380 KB |
Output is correct |
49 |
Correct |
9 ms |
7508 KB |
Output is correct |
50 |
Correct |
8 ms |
7380 KB |
Output is correct |
51 |
Correct |
77 ms |
8792 KB |
Output is correct |
52 |
Correct |
71 ms |
8808 KB |
Output is correct |
53 |
Correct |
20 ms |
7748 KB |
Output is correct |
54 |
Correct |
6 ms |
6996 KB |
Output is correct |
55 |
Correct |
7 ms |
6996 KB |
Output is correct |
56 |
Correct |
15 ms |
8144 KB |
Output is correct |
57 |
Correct |
132 ms |
7904 KB |
Output is correct |
58 |
Correct |
15 ms |
7432 KB |
Output is correct |
59 |
Correct |
18 ms |
7512 KB |
Output is correct |
60 |
Correct |
20 ms |
7508 KB |
Output is correct |
61 |
Correct |
16 ms |
7508 KB |
Output is correct |
62 |
Correct |
29 ms |
8208 KB |
Output is correct |
63 |
Correct |
63 ms |
9312 KB |
Output is correct |
64 |
Correct |
67 ms |
9244 KB |
Output is correct |
65 |
Correct |
87 ms |
9316 KB |
Output is correct |
66 |
Correct |
441 ms |
9416 KB |
Output is correct |
67 |
Correct |
423 ms |
9652 KB |
Output is correct |