#include <bits/stdc++.h>
using namespace std;
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)
namespace IO {
const int SIZE = 1 << 23;
char ibuf[SIZE], *iS, *iT;
char obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
bool flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
return true;
}
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)
#define pc(x) (*oS++ = x)
int read() {
int x = 0, f = 0;
char c = gc();
while (!isdigit(c)) f |= c == '-', c = gc();
while (isdigit(c)) x = 10 * x + c - '0', c = gc();
return f ? -x : x;
}
char qu[55]; int qlen;
template <class T> void print(T x) {
if (!x) pc('0');
if (x < 0) pc('-'), x = -x;
while (x) qu[++qlen] = x % 10 + '0', x /= 10;
while (qlen) pc(qu[qlen--]);
}
template <class T> void print(T x, char charset) {
print(x), pc(charset);
}
struct Flusher {
~Flusher() {
flush();
}
} flusher;
}
using namespace IO;
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];
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();
// cout<<x<<" "<<dist<<" "<<val<<" "<<q.size()<<endl;
if (f[x][dist]!=val) continue;
if (!First[x])
{
First[x]=true;
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);
}
for (int xnew:num[x])
{
int dem=0;
for (int i=x+p[xnew];i<=n;i+=p[xnew])
{
// cout<<i<<endl;
dem++;
update(i,0,val+dem);
}
dem=0;
for (int i=x-p[xnew];i>=1;i-=p[xnew])
{
//cout<<i<<endl;
dem++;
update(i,0,val+dem);
}
}
// break;
}
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;
n=read();
m=read();
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++)
{
a[i]=read();
p[i]=read();
// cin>>a[i]>>p[i];
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 |
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 |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
0 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 |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
2 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 |
2 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 |
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 |
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 |
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 |
1228 KB |
Output is correct |
17 |
Correct |
2 ms |
1740 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
3 ms |
2252 KB |
Output is correct |
24 |
Correct |
4 ms |
2508 KB |
Output is correct |
25 |
Correct |
3 ms |
2380 KB |
Output is correct |
26 |
Correct |
2 ms |
2508 KB |
Output is correct |
27 |
Correct |
2 ms |
2380 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
8 ms |
2380 KB |
Output is correct |
30 |
Correct |
3 ms |
2380 KB |
Output is correct |
31 |
Correct |
6 ms |
2380 KB |
Output is correct |
32 |
Correct |
3 ms |
2508 KB |
Output is correct |
33 |
Correct |
14 ms |
2652 KB |
Output is correct |
34 |
Correct |
14 ms |
2648 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 |
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 |
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 |
1228 KB |
Output is correct |
17 |
Correct |
3 ms |
1740 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2764 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
1 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2252 KB |
Output is correct |
24 |
Correct |
3 ms |
2508 KB |
Output is correct |
25 |
Correct |
2 ms |
2472 KB |
Output is correct |
26 |
Correct |
2 ms |
2508 KB |
Output is correct |
27 |
Correct |
1 ms |
2380 KB |
Output is correct |
28 |
Correct |
2 ms |
2508 KB |
Output is correct |
29 |
Correct |
8 ms |
2380 KB |
Output is correct |
30 |
Correct |
3 ms |
2380 KB |
Output is correct |
31 |
Correct |
6 ms |
2488 KB |
Output is correct |
32 |
Correct |
3 ms |
2508 KB |
Output is correct |
33 |
Correct |
14 ms |
2636 KB |
Output is correct |
34 |
Correct |
14 ms |
2652 KB |
Output is correct |
35 |
Correct |
17 ms |
2892 KB |
Output is correct |
36 |
Correct |
3 ms |
2124 KB |
Output is correct |
37 |
Correct |
37 ms |
3276 KB |
Output is correct |
38 |
Correct |
32 ms |
3592 KB |
Output is correct |
39 |
Correct |
36 ms |
3568 KB |
Output is correct |
40 |
Correct |
26 ms |
3592 KB |
Output is correct |
41 |
Correct |
27 ms |
3532 KB |
Output is correct |
42 |
Correct |
3 ms |
2892 KB |
Output is correct |
43 |
Correct |
3 ms |
2892 KB |
Output is correct |
44 |
Correct |
3 ms |
3148 KB |
Output is correct |
45 |
Correct |
58 ms |
3248 KB |
Output is correct |
46 |
Correct |
58 ms |
3252 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 |
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 |
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 |
1228 KB |
Output is correct |
17 |
Correct |
3 ms |
1740 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2776 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2252 KB |
Output is correct |
24 |
Correct |
2 ms |
2508 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
2 ms |
2508 KB |
Output is correct |
27 |
Correct |
2 ms |
2380 KB |
Output is correct |
28 |
Correct |
2 ms |
2508 KB |
Output is correct |
29 |
Correct |
8 ms |
2380 KB |
Output is correct |
30 |
Correct |
3 ms |
2380 KB |
Output is correct |
31 |
Correct |
5 ms |
2380 KB |
Output is correct |
32 |
Correct |
3 ms |
2508 KB |
Output is correct |
33 |
Correct |
14 ms |
2652 KB |
Output is correct |
34 |
Correct |
13 ms |
2636 KB |
Output is correct |
35 |
Correct |
21 ms |
2976 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
33 ms |
3276 KB |
Output is correct |
38 |
Correct |
29 ms |
3532 KB |
Output is correct |
39 |
Correct |
27 ms |
3532 KB |
Output is correct |
40 |
Correct |
27 ms |
3592 KB |
Output is correct |
41 |
Correct |
27 ms |
3532 KB |
Output is correct |
42 |
Correct |
2 ms |
2892 KB |
Output is correct |
43 |
Correct |
2 ms |
2892 KB |
Output is correct |
44 |
Correct |
2 ms |
3148 KB |
Output is correct |
45 |
Correct |
53 ms |
3248 KB |
Output is correct |
46 |
Correct |
56 ms |
3372 KB |
Output is correct |
47 |
Correct |
133 ms |
12684 KB |
Output is correct |
48 |
Correct |
11 ms |
18380 KB |
Output is correct |
49 |
Correct |
12 ms |
19660 KB |
Output is correct |
50 |
Correct |
12 ms |
21196 KB |
Output is correct |
51 |
Correct |
67 ms |
23932 KB |
Output is correct |
52 |
Correct |
69 ms |
24080 KB |
Output is correct |
53 |
Correct |
24 ms |
22900 KB |
Output is correct |
54 |
Correct |
15 ms |
21708 KB |
Output is correct |
55 |
Correct |
16 ms |
21708 KB |
Output is correct |
56 |
Correct |
23 ms |
27436 KB |
Output is correct |
57 |
Correct |
92 ms |
26128 KB |
Output is correct |
58 |
Correct |
29 ms |
22060 KB |
Output is correct |
59 |
Correct |
24 ms |
22272 KB |
Output is correct |
60 |
Correct |
34 ms |
22492 KB |
Output is correct |
61 |
Correct |
25 ms |
22328 KB |
Output is correct |
62 |
Correct |
61 ms |
23244 KB |
Output is correct |
63 |
Correct |
69 ms |
24004 KB |
Output is correct |
64 |
Correct |
84 ms |
24160 KB |
Output is correct |
65 |
Execution timed out |
1014 ms |
24152 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |