#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+9;
const int oo=1e9+9;
typedef pair <int,int> ii;
typedef pair <int,ii> iii;
int can,n,m,a[N],p[N],f[N][176];
bool First[N],check[N][176],doge[N];
vector <int> num[N];
priority_queue <iii,vector<iii>,greater<iii> > q;
void update(int x,int dist,int val)
{
if (f[x][dist]>val)
{
f[x][dist]=val;
q.push({val,{x,dist}});
}
if (num[x].size()==0||dist==0) return;
}
int solve()
{
if (p[1]>=can)
{
f[a[1]][0]=0;
q.push({0,{a[1],0}});
}
else
{
f[a[1]][p[1]]=0;
q.push({0,{a[1],p[1]}});
}
int val,x,dist;
while (!q.empty())
{
val=q.top().first;
x=q.top().second.first;
dist=q.top().second.second;
q.pop();
if (f[x][dist]!=val) continue;
if (!First[x])
{
for (int i=0;i<can;i++)
if (check[x][i]||i==0) update(x,i,val);
}
if (dist!=0)
{
if (x+dist<=n) update(x+dist,dist,f[x][dist]+1);
if (x-dist>=1) update(x-dist,dist,f[x][dist]+1);
}
if (First[x]) continue;
First[x]=true;
for (int xnew:num[x])
{
int dem=0;
for (int i=x;i<=n;i+=p[xnew])
{
update(i,0,val+dem);
dem++;
}
dem=0;
for (int i=x;i>=1;i-=p[xnew])
{
update(i,0,val+dem);
dem++;
}
}
}
int res=oo;
for (int i=0;i<=can;i++)
res=min(res,f[a[2]][i]);
if (res==oo) res=-1;
return res;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("test.inp","r",stdin);
cin>>n>>m;
can=sqrt(n);
for (int i=0;i<=n;i++)
for (int j=0;j<=can;j++)
f[i][j]=oo;
for (int i=1;i<=m;i++)
{
cin>>a[i]>>p[i];
doge[a[i]]=true;
a[i]++;
if (p[i]>=can) num[a[i]].push_back(i);
else check[a[i]][p[i]]=true;
}
cout<<solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
968 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 |
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 |
# |
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 |
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 |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 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 |
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 |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
2252 KB |
Output is correct |
18 |
Correct |
2 ms |
3532 KB |
Output is correct |
19 |
Correct |
2 ms |
3788 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
1 ms |
1612 KB |
Output is correct |
22 |
Correct |
2 ms |
3532 KB |
Output is correct |
23 |
Correct |
2 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3848 KB |
Output is correct |
25 |
Correct |
3 ms |
3788 KB |
Output is correct |
26 |
Correct |
3 ms |
3916 KB |
Output is correct |
27 |
Correct |
3 ms |
3788 KB |
Output is correct |
28 |
Correct |
3 ms |
3916 KB |
Output is correct |
29 |
Correct |
9 ms |
3824 KB |
Output is correct |
30 |
Correct |
5 ms |
3716 KB |
Output is correct |
31 |
Correct |
7 ms |
3936 KB |
Output is correct |
32 |
Correct |
3 ms |
3916 KB |
Output is correct |
33 |
Correct |
12 ms |
4172 KB |
Output is correct |
34 |
Correct |
13 ms |
4300 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 |
948 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 |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
2 ms |
2252 KB |
Output is correct |
18 |
Correct |
2 ms |
3532 KB |
Output is correct |
19 |
Correct |
2 ms |
3788 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
1 ms |
1612 KB |
Output is correct |
22 |
Correct |
2 ms |
3532 KB |
Output is correct |
23 |
Correct |
3 ms |
3276 KB |
Output is correct |
24 |
Correct |
4 ms |
3832 KB |
Output is correct |
25 |
Correct |
3 ms |
3916 KB |
Output is correct |
26 |
Correct |
3 ms |
3916 KB |
Output is correct |
27 |
Correct |
2 ms |
3788 KB |
Output is correct |
28 |
Correct |
3 ms |
3916 KB |
Output is correct |
29 |
Correct |
8 ms |
3896 KB |
Output is correct |
30 |
Correct |
4 ms |
3788 KB |
Output is correct |
31 |
Correct |
6 ms |
3916 KB |
Output is correct |
32 |
Correct |
3 ms |
3916 KB |
Output is correct |
33 |
Correct |
11 ms |
4172 KB |
Output is correct |
34 |
Correct |
11 ms |
4172 KB |
Output is correct |
35 |
Correct |
11 ms |
4364 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
14 ms |
4940 KB |
Output is correct |
38 |
Correct |
14 ms |
5392 KB |
Output is correct |
39 |
Correct |
14 ms |
5452 KB |
Output is correct |
40 |
Correct |
17 ms |
5448 KB |
Output is correct |
41 |
Correct |
14 ms |
5452 KB |
Output is correct |
42 |
Correct |
6 ms |
4428 KB |
Output is correct |
43 |
Correct |
10 ms |
4512 KB |
Output is correct |
44 |
Correct |
10 ms |
4748 KB |
Output is correct |
45 |
Correct |
17 ms |
5196 KB |
Output is correct |
46 |
Correct |
17 ms |
5196 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 |
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 |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
2 ms |
2252 KB |
Output is correct |
18 |
Correct |
2 ms |
3532 KB |
Output is correct |
19 |
Correct |
2 ms |
3788 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
1 ms |
1612 KB |
Output is correct |
22 |
Correct |
2 ms |
3532 KB |
Output is correct |
23 |
Correct |
2 ms |
3276 KB |
Output is correct |
24 |
Correct |
4 ms |
3916 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
3 ms |
3916 KB |
Output is correct |
27 |
Correct |
3 ms |
3788 KB |
Output is correct |
28 |
Correct |
3 ms |
3916 KB |
Output is correct |
29 |
Correct |
8 ms |
3844 KB |
Output is correct |
30 |
Correct |
3 ms |
3788 KB |
Output is correct |
31 |
Correct |
6 ms |
3916 KB |
Output is correct |
32 |
Correct |
5 ms |
4044 KB |
Output is correct |
33 |
Correct |
16 ms |
4180 KB |
Output is correct |
34 |
Correct |
13 ms |
4172 KB |
Output is correct |
35 |
Correct |
10 ms |
4300 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
13 ms |
4868 KB |
Output is correct |
38 |
Correct |
14 ms |
5476 KB |
Output is correct |
39 |
Correct |
17 ms |
5372 KB |
Output is correct |
40 |
Correct |
14 ms |
5452 KB |
Output is correct |
41 |
Correct |
15 ms |
5484 KB |
Output is correct |
42 |
Correct |
6 ms |
4440 KB |
Output is correct |
43 |
Correct |
6 ms |
4428 KB |
Output is correct |
44 |
Correct |
6 ms |
4752 KB |
Output is correct |
45 |
Correct |
16 ms |
5196 KB |
Output is correct |
46 |
Correct |
20 ms |
5196 KB |
Output is correct |
47 |
Correct |
83 ms |
21836 KB |
Output is correct |
48 |
Correct |
18 ms |
34572 KB |
Output is correct |
49 |
Correct |
21 ms |
37380 KB |
Output is correct |
50 |
Correct |
21 ms |
40516 KB |
Output is correct |
51 |
Correct |
70 ms |
45252 KB |
Output is correct |
52 |
Correct |
78 ms |
45448 KB |
Output is correct |
53 |
Correct |
37 ms |
43856 KB |
Output is correct |
54 |
Correct |
23 ms |
42316 KB |
Output is correct |
55 |
Correct |
26 ms |
42476 KB |
Output is correct |
56 |
Correct |
36 ms |
48136 KB |
Output is correct |
57 |
Correct |
93 ms |
46860 KB |
Output is correct |
58 |
Correct |
36 ms |
42972 KB |
Output is correct |
59 |
Correct |
35 ms |
43144 KB |
Output is correct |
60 |
Correct |
43 ms |
43336 KB |
Output is correct |
61 |
Correct |
38 ms |
43268 KB |
Output is correct |
62 |
Correct |
61 ms |
44156 KB |
Output is correct |
63 |
Correct |
68 ms |
46780 KB |
Output is correct |
64 |
Correct |
75 ms |
46644 KB |
Output is correct |
65 |
Correct |
314 ms |
46588 KB |
Output is correct |
66 |
Correct |
904 ms |
49600 KB |
Output is correct |
67 |
Correct |
881 ms |
49664 KB |
Output is correct |