#include <bits/stdc++.h>
#define quan160607 "BoundSeq"
#define fi first
#define se second
#define pii pair<int, int>
#define piii pair<int, pii>
#define pb push_back
using namespace std;
typedef long long ll;
const ll oo = 1e18;
const int inf = 2e9;
const int nmax = 3e4 + 5;
void start()
{
// freopen(quan160607".inp","r",stdin);
// freopen(quan160607".out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, m;
int b[nmax], a[nmax], p[nmax];
vector<int> v[nmax];
const int block = 100;
int dp[nmax][block + 5];
priority_queue<piii, vector<piii>, greater<piii>> q;
bool minimize(int &a, int b)
{
if(a > b)
{
a = b;
return true;
} else return false;
}
int ans = inf;
void solve_dij()
{
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= block; j++)
dp[i][j] = inf;
}
dp[b[0]][0] = 0;
q.push({dp[b[0]][0], {b[0], 0}});
while(!q.empty())
{
piii temp = q.top();
q.pop();
int u = temp.se.fi, pk = temp.se.se;
if(u == b[1]) ans = min(ans, dp[u][pk]);
if(temp.fi != dp[u][pk])
continue;
if(pk > 0)
{
if(u - pk >= 0) {
if(minimize(dp[u - pk][pk], dp[u][pk] + 1))
q.push({dp[u - pk][pk], {u - pk, pk}});
}
if(u + pk < n) {
if(minimize(dp[u + pk][pk], dp[u][pk] + 1))
q.push({dp[u + pk][pk], {u + pk, pk}});
}
if(dp[u][0] > dp[u][pk])
{
dp[u][0] = dp[u][pk];
q.push({dp[u][0], {u, 0}});
}
}
else if(pk == 0) {
for(auto x : v[u])
{
if(x > block)
{
int cnt = 0;
for(int i = u; i < n; i += x) {
if(minimize(dp[i][0], temp.fi + cnt)) {
q.push({dp[i][0], {i, 0}});
}
cnt++;
}
cnt = 0;
for(int i = u; i >= 0; i -= x) {
if(minimize(dp[i][0], temp.fi + cnt)) {
q.push({dp[i][0], {i, 0}});
}
cnt++;
}
}
else if(x <= block) {
if(minimize(dp[u][x], dp[u][pk]))
q.push({dp[u][x], {u, x}});
}
}
}
}
}
int main()
{
start();
cin >> n >> m;
for(int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
v[b[i]].push_back(p[i]);
}
solve_dij();
cout << (ans == inf ? -1 : ans);
// cout << "\n";
// cout << dp[2][0] << " " << dp[2][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2904 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2904 KB |
Output is correct |
7 |
Correct |
1 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2904 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
1 ms |
3160 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
1 ms |
2904 KB |
Output is correct |
11 |
Correct |
2 ms |
3172 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2904 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
2 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2904 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2904 KB |
Output is correct |
7 |
Correct |
1 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
2904 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
1 ms |
2904 KB |
Output is correct |
11 |
Correct |
2 ms |
3160 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
3160 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
2 ms |
3160 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
1 ms |
5208 KB |
Output is correct |
19 |
Correct |
1 ms |
5208 KB |
Output is correct |
20 |
Correct |
2 ms |
5208 KB |
Output is correct |
21 |
Correct |
1 ms |
3160 KB |
Output is correct |
22 |
Correct |
1 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
1 ms |
5208 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
6 ms |
5208 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
5 ms |
5208 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
13 ms |
5208 KB |
Output is correct |
34 |
Correct |
13 ms |
5360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2904 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2904 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
1 ms |
2904 KB |
Output is correct |
11 |
Correct |
2 ms |
3160 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2904 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
2 ms |
3160 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
1 ms |
5208 KB |
Output is correct |
19 |
Correct |
1 ms |
5212 KB |
Output is correct |
20 |
Correct |
1 ms |
5208 KB |
Output is correct |
21 |
Correct |
1 ms |
3416 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5208 KB |
Output is correct |
24 |
Correct |
2 ms |
5208 KB |
Output is correct |
25 |
Correct |
2 ms |
5208 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
1 ms |
5208 KB |
Output is correct |
28 |
Correct |
2 ms |
5208 KB |
Output is correct |
29 |
Correct |
7 ms |
5208 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5208 KB |
Output is correct |
33 |
Correct |
12 ms |
5208 KB |
Output is correct |
34 |
Correct |
12 ms |
5208 KB |
Output is correct |
35 |
Correct |
10 ms |
5720 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
13 ms |
5720 KB |
Output is correct |
38 |
Correct |
13 ms |
5724 KB |
Output is correct |
39 |
Correct |
14 ms |
5720 KB |
Output is correct |
40 |
Correct |
13 ms |
5720 KB |
Output is correct |
41 |
Correct |
14 ms |
5720 KB |
Output is correct |
42 |
Correct |
4 ms |
5724 KB |
Output is correct |
43 |
Correct |
4 ms |
5720 KB |
Output is correct |
44 |
Correct |
4 ms |
5720 KB |
Output is correct |
45 |
Correct |
25 ms |
5976 KB |
Output is correct |
46 |
Correct |
25 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
2904 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2904 KB |
Output is correct |
8 |
Correct |
1 ms |
2904 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
3164 KB |
Output is correct |
12 |
Correct |
1 ms |
3104 KB |
Output is correct |
13 |
Correct |
1 ms |
2904 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
2 ms |
3160 KB |
Output is correct |
16 |
Correct |
1 ms |
2904 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
1 ms |
5208 KB |
Output is correct |
19 |
Correct |
1 ms |
5212 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
21 |
Correct |
1 ms |
3160 KB |
Output is correct |
22 |
Correct |
1 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5208 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5208 KB |
Output is correct |
27 |
Correct |
1 ms |
5208 KB |
Output is correct |
28 |
Correct |
2 ms |
5208 KB |
Output is correct |
29 |
Correct |
6 ms |
5208 KB |
Output is correct |
30 |
Correct |
3 ms |
5208 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
12 ms |
5212 KB |
Output is correct |
34 |
Correct |
12 ms |
5212 KB |
Output is correct |
35 |
Correct |
10 ms |
5720 KB |
Output is correct |
36 |
Correct |
3 ms |
5208 KB |
Output is correct |
37 |
Correct |
13 ms |
5720 KB |
Output is correct |
38 |
Correct |
14 ms |
5720 KB |
Output is correct |
39 |
Correct |
13 ms |
5720 KB |
Output is correct |
40 |
Correct |
15 ms |
5720 KB |
Output is correct |
41 |
Correct |
13 ms |
5720 KB |
Output is correct |
42 |
Correct |
5 ms |
5720 KB |
Output is correct |
43 |
Correct |
5 ms |
5720 KB |
Output is correct |
44 |
Correct |
4 ms |
5720 KB |
Output is correct |
45 |
Correct |
25 ms |
5976 KB |
Output is correct |
46 |
Correct |
25 ms |
5976 KB |
Output is correct |
47 |
Correct |
49 ms |
10324 KB |
Output is correct |
48 |
Correct |
6 ms |
13924 KB |
Output is correct |
49 |
Correct |
6 ms |
13916 KB |
Output is correct |
50 |
Correct |
5 ms |
13912 KB |
Output is correct |
51 |
Correct |
32 ms |
14676 KB |
Output is correct |
52 |
Correct |
31 ms |
14676 KB |
Output is correct |
53 |
Correct |
11 ms |
14168 KB |
Output is correct |
54 |
Correct |
5 ms |
13400 KB |
Output is correct |
55 |
Correct |
4 ms |
13400 KB |
Output is correct |
56 |
Correct |
10 ms |
14428 KB |
Output is correct |
57 |
Correct |
51 ms |
13400 KB |
Output is correct |
58 |
Correct |
7 ms |
13912 KB |
Output is correct |
59 |
Correct |
9 ms |
13912 KB |
Output is correct |
60 |
Correct |
10 ms |
13912 KB |
Output is correct |
61 |
Correct |
9 ms |
13912 KB |
Output is correct |
62 |
Correct |
16 ms |
14424 KB |
Output is correct |
63 |
Correct |
27 ms |
15576 KB |
Output is correct |
64 |
Correct |
29 ms |
15572 KB |
Output is correct |
65 |
Correct |
42 ms |
17012 KB |
Output is correct |
66 |
Correct |
381 ms |
17612 KB |
Output is correct |
67 |
Correct |
355 ms |
17104 KB |
Output is correct |