#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f1 first
#define s2 second
#define fastio ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(x) cerr << "[" << #x << "]: " << x << "\n";
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
constexpr ld PI = 4*atan((ld)1);
constexpr int MAX = 30069;
constexpr int SQ = 100;
constexpr int inf = 1e9 + 69;
int n, m;
int cost[MAX][SQ];
int b[MAX], p[MAX];
vector<int> adj[MAX];
int main()
{
fastio;
for (int i = 0; i < MAX; ++i)
for (int j = 0; j < SQ; ++j)
cost[i][j] = inf;
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
cin >> b[i] >> p[i];
if (i) adj[b[i]].pb(p[i]);
}
const auto valid = [&](int pos) -> bool
{
return 0 <= pos && pos < n;
};
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> pq;
if (p[0] < SQ)
{
cost[b[0]][0] = cost[b[0]][p[0]] = 0;
pq.push({0, p[0], b[0]});
}
else
{
for (int i = b[0], ctr = 0; i >= 0; i -= p[0], ++ctr)
{
if (cost[i][0] > ctr)
cost[i][0] = ctr, pq.push({ctr, 0, i});
}
for (int i = b[0], ctr = 0; i < n; i += p[0], ++ctr)
{
if (cost[i][0] > ctr)
cost[i][0] = ctr, pq.push({ctr, 0, i});
}
}
while (!pq.empty())
{
auto [c, mov, pos] = pq.top();
pq.pop();
if (cost[pos][mov] < c)
continue;
if (mov)
{
if (valid(pos - mov) && cost[pos - mov][mov] > 1 + c)
{
cost[pos - mov][0] = min(cost[pos - mov][0], 1 + c);
cost[pos - mov][mov] = 1 + c;
pq.push({1 + c, mov, pos - mov});
}
if (valid(pos + mov) && cost[pos + mov][mov] > 1 + c)
{
cost[pos + mov][0] = min(cost[pos + mov][0], 1 + c);
cost[pos + mov][mov] = 1 + c;
pq.push({1 + c, mov, pos + mov});
}
}
for (int x : adj[pos])
{
if (x < SQ)
{
if (valid(pos - x) && cost[pos - x][x] > 1 + c)
{
cost[pos - x][0] = min(cost[pos - x][0], 1 + c);
cost[pos - x][x] = 1 + c;
pq.push({1 + c, x, pos - x});
}
if (valid(pos + x) && cost[pos + x][x] > 1 + c)
{
cost[pos + x][0] = min(cost[pos + x][0], 1 + c);
cost[pos + x][x] = 1 + c;
pq.push({1 + c, x, pos + x});
}
}
else
{
for (int i = pos, ctr = c; i >= 0; i -= x, ++ctr)
{
if (cost[i][0] > ctr)
cost[i][0] = ctr, pq.push({ctr, 0, i});
}
for (int i = pos, ctr = c; i < n; i += x, ++ctr)
{
if (cost[i][0] > ctr)
cost[i][0] = ctr, pq.push({ctr, 0, i});
}
}
}
adj[pos].clear();
}
cout << (cost[b[1]][0] == inf ? -1 : cost[b[1]][0]) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12748 KB |
Output is correct |
2 |
Correct |
8 ms |
12736 KB |
Output is correct |
3 |
Correct |
6 ms |
12748 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
6 ms |
12748 KB |
Output is correct |
6 |
Correct |
7 ms |
12716 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12796 KB |
Output is correct |
2 |
Correct |
6 ms |
12748 KB |
Output is correct |
3 |
Correct |
6 ms |
12688 KB |
Output is correct |
4 |
Correct |
12 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
7 ms |
12760 KB |
Output is correct |
7 |
Correct |
13 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12708 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12796 KB |
Output is correct |
11 |
Correct |
8 ms |
12792 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12792 KB |
Output is correct |
14 |
Correct |
8 ms |
12876 KB |
Output is correct |
15 |
Correct |
8 ms |
12804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12796 KB |
Output is correct |
2 |
Correct |
7 ms |
12748 KB |
Output is correct |
3 |
Correct |
9 ms |
12800 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12680 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
10 ms |
12768 KB |
Output is correct |
8 |
Correct |
10 ms |
12748 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
10 ms |
12748 KB |
Output is correct |
11 |
Correct |
8 ms |
12808 KB |
Output is correct |
12 |
Correct |
10 ms |
12748 KB |
Output is correct |
13 |
Correct |
7 ms |
12748 KB |
Output is correct |
14 |
Correct |
8 ms |
12876 KB |
Output is correct |
15 |
Correct |
11 ms |
13000 KB |
Output is correct |
16 |
Correct |
8 ms |
12824 KB |
Output is correct |
17 |
Correct |
10 ms |
12808 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
7 ms |
12748 KB |
Output is correct |
20 |
Correct |
7 ms |
12876 KB |
Output is correct |
21 |
Correct |
7 ms |
12800 KB |
Output is correct |
22 |
Correct |
8 ms |
12804 KB |
Output is correct |
23 |
Correct |
8 ms |
12808 KB |
Output is correct |
24 |
Correct |
9 ms |
12804 KB |
Output is correct |
25 |
Correct |
11 ms |
12848 KB |
Output is correct |
26 |
Correct |
10 ms |
12760 KB |
Output is correct |
27 |
Correct |
8 ms |
12832 KB |
Output is correct |
28 |
Correct |
12 ms |
12832 KB |
Output is correct |
29 |
Correct |
16 ms |
12748 KB |
Output is correct |
30 |
Correct |
9 ms |
12748 KB |
Output is correct |
31 |
Correct |
15 ms |
12748 KB |
Output is correct |
32 |
Correct |
12 ms |
12748 KB |
Output is correct |
33 |
Correct |
26 ms |
12904 KB |
Output is correct |
34 |
Correct |
27 ms |
12936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12720 KB |
Output is correct |
2 |
Correct |
9 ms |
12744 KB |
Output is correct |
3 |
Correct |
9 ms |
12748 KB |
Output is correct |
4 |
Correct |
8 ms |
12792 KB |
Output is correct |
5 |
Correct |
9 ms |
12784 KB |
Output is correct |
6 |
Correct |
8 ms |
12800 KB |
Output is correct |
7 |
Correct |
8 ms |
12776 KB |
Output is correct |
8 |
Correct |
8 ms |
12684 KB |
Output is correct |
9 |
Correct |
10 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
8 ms |
12888 KB |
Output is correct |
12 |
Correct |
7 ms |
12748 KB |
Output is correct |
13 |
Correct |
8 ms |
12792 KB |
Output is correct |
14 |
Correct |
8 ms |
12800 KB |
Output is correct |
15 |
Correct |
9 ms |
12880 KB |
Output is correct |
16 |
Correct |
9 ms |
12748 KB |
Output is correct |
17 |
Correct |
13 ms |
12812 KB |
Output is correct |
18 |
Correct |
9 ms |
12808 KB |
Output is correct |
19 |
Correct |
9 ms |
12724 KB |
Output is correct |
20 |
Correct |
8 ms |
12840 KB |
Output is correct |
21 |
Correct |
9 ms |
12788 KB |
Output is correct |
22 |
Correct |
7 ms |
12796 KB |
Output is correct |
23 |
Correct |
8 ms |
12752 KB |
Output is correct |
24 |
Correct |
9 ms |
12816 KB |
Output is correct |
25 |
Correct |
11 ms |
12812 KB |
Output is correct |
26 |
Correct |
8 ms |
12800 KB |
Output is correct |
27 |
Correct |
11 ms |
12804 KB |
Output is correct |
28 |
Correct |
11 ms |
12880 KB |
Output is correct |
29 |
Correct |
15 ms |
12748 KB |
Output is correct |
30 |
Correct |
12 ms |
12796 KB |
Output is correct |
31 |
Correct |
12 ms |
12756 KB |
Output is correct |
32 |
Correct |
12 ms |
12836 KB |
Output is correct |
33 |
Correct |
24 ms |
12880 KB |
Output is correct |
34 |
Correct |
22 ms |
12940 KB |
Output is correct |
35 |
Correct |
26 ms |
13528 KB |
Output is correct |
36 |
Correct |
13 ms |
12860 KB |
Output is correct |
37 |
Correct |
25 ms |
13432 KB |
Output is correct |
38 |
Correct |
25 ms |
13656 KB |
Output is correct |
39 |
Correct |
30 ms |
13576 KB |
Output is correct |
40 |
Correct |
26 ms |
13740 KB |
Output is correct |
41 |
Correct |
23 ms |
13576 KB |
Output is correct |
42 |
Correct |
12 ms |
13384 KB |
Output is correct |
43 |
Correct |
11 ms |
13388 KB |
Output is correct |
44 |
Correct |
10 ms |
13388 KB |
Output is correct |
45 |
Correct |
50 ms |
13688 KB |
Output is correct |
46 |
Correct |
41 ms |
13700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12752 KB |
Output is correct |
2 |
Correct |
7 ms |
12800 KB |
Output is correct |
3 |
Correct |
8 ms |
12796 KB |
Output is correct |
4 |
Correct |
8 ms |
12796 KB |
Output is correct |
5 |
Correct |
8 ms |
12796 KB |
Output is correct |
6 |
Correct |
11 ms |
12800 KB |
Output is correct |
7 |
Correct |
7 ms |
12752 KB |
Output is correct |
8 |
Correct |
7 ms |
12748 KB |
Output is correct |
9 |
Correct |
7 ms |
12764 KB |
Output is correct |
10 |
Correct |
8 ms |
12736 KB |
Output is correct |
11 |
Correct |
9 ms |
12812 KB |
Output is correct |
12 |
Correct |
12 ms |
12736 KB |
Output is correct |
13 |
Correct |
8 ms |
12748 KB |
Output is correct |
14 |
Correct |
8 ms |
12876 KB |
Output is correct |
15 |
Correct |
15 ms |
12848 KB |
Output is correct |
16 |
Correct |
7 ms |
12740 KB |
Output is correct |
17 |
Correct |
9 ms |
12876 KB |
Output is correct |
18 |
Correct |
8 ms |
12748 KB |
Output is correct |
19 |
Correct |
8 ms |
12748 KB |
Output is correct |
20 |
Correct |
15 ms |
12828 KB |
Output is correct |
21 |
Correct |
16 ms |
12748 KB |
Output is correct |
22 |
Correct |
10 ms |
12792 KB |
Output is correct |
23 |
Correct |
8 ms |
12800 KB |
Output is correct |
24 |
Correct |
9 ms |
12876 KB |
Output is correct |
25 |
Correct |
8 ms |
12808 KB |
Output is correct |
26 |
Correct |
8 ms |
12748 KB |
Output is correct |
27 |
Correct |
8 ms |
12804 KB |
Output is correct |
28 |
Correct |
17 ms |
12888 KB |
Output is correct |
29 |
Correct |
15 ms |
12924 KB |
Output is correct |
30 |
Correct |
9 ms |
12748 KB |
Output is correct |
31 |
Correct |
12 ms |
12812 KB |
Output is correct |
32 |
Correct |
12 ms |
12832 KB |
Output is correct |
33 |
Correct |
23 ms |
12932 KB |
Output is correct |
34 |
Correct |
22 ms |
12876 KB |
Output is correct |
35 |
Correct |
18 ms |
13508 KB |
Output is correct |
36 |
Correct |
10 ms |
12960 KB |
Output is correct |
37 |
Correct |
24 ms |
13456 KB |
Output is correct |
38 |
Correct |
25 ms |
13676 KB |
Output is correct |
39 |
Correct |
24 ms |
13644 KB |
Output is correct |
40 |
Correct |
37 ms |
13640 KB |
Output is correct |
41 |
Correct |
23 ms |
13652 KB |
Output is correct |
42 |
Correct |
11 ms |
13376 KB |
Output is correct |
43 |
Correct |
11 ms |
13388 KB |
Output is correct |
44 |
Correct |
34 ms |
13460 KB |
Output is correct |
45 |
Correct |
41 ms |
13796 KB |
Output is correct |
46 |
Correct |
51 ms |
13704 KB |
Output is correct |
47 |
Correct |
83 ms |
14092 KB |
Output is correct |
48 |
Correct |
13 ms |
13644 KB |
Output is correct |
49 |
Correct |
12 ms |
13644 KB |
Output is correct |
50 |
Correct |
13 ms |
13376 KB |
Output is correct |
51 |
Correct |
55 ms |
14408 KB |
Output is correct |
52 |
Correct |
61 ms |
14476 KB |
Output is correct |
53 |
Correct |
18 ms |
13900 KB |
Output is correct |
54 |
Correct |
10 ms |
12680 KB |
Output is correct |
55 |
Correct |
9 ms |
12748 KB |
Output is correct |
56 |
Correct |
16 ms |
14088 KB |
Output is correct |
57 |
Correct |
120 ms |
12916 KB |
Output is correct |
58 |
Correct |
14 ms |
13284 KB |
Output is correct |
59 |
Correct |
17 ms |
13440 KB |
Output is correct |
60 |
Correct |
23 ms |
13388 KB |
Output is correct |
61 |
Correct |
18 ms |
13440 KB |
Output is correct |
62 |
Correct |
32 ms |
14200 KB |
Output is correct |
63 |
Correct |
61 ms |
15088 KB |
Output is correct |
64 |
Correct |
55 ms |
15040 KB |
Output is correct |
65 |
Correct |
70 ms |
15100 KB |
Output is correct |
66 |
Correct |
494 ms |
16616 KB |
Output is correct |
67 |
Correct |
507 ms |
16600 KB |
Output is correct |