#include <bits/stdc++.h>
using namespace std;
struct pii {
int x, i;
bool operator<(const pii b) const { return x>b.x; }
};
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
int N, M, st=0, ed=-1;
cin>>N>>M;
vector<int> D(N, INT_MAX);
vector<vector<int>> A(N);
for(int i=0; i<M; i++) {
int x, m;
cin>>x>>m;
if(i==0) st=x;
else if(i==1) ed=x;
if(max(x, N-1-x)>=m) A[x].push_back(m);
}
for(int i=0; i<N; i++) {
sort(rbegin(A[i]), rend(A[i]));
A[i].erase(unique(begin(A[i]), end(A[i])), end(A[i]));
}
priority_queue<pii> q;
q.push({0, st}); D[st]=0;
while(q.size()) {
auto[n,x]=q.top(); q.pop();
if(D[x]!=n) continue;
if(x==ed) { cout<<n; return 0; }
for(int m:A[x]) {
for(int i=x+m, d=n+1; i<N; i+=m, d++) if(d<D[i]) {
D[i]=d, q.push({d, i});
if(binary_search(rbegin(A[i]), rend(A[i]), m)) break;
}
for(int i=x-m, d=n+1; i>=0; i-=m, d++) if(d<D[i]) {
D[i]=d, q.push({d, i});
if(binary_search(rbegin(A[i]), rend(A[i]), m)) break;
}
}
}
cout<<-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
6 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
392 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
380 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
7 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
6 ms |
376 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
6 ms |
376 KB |
Output is correct |
29 |
Correct |
6 ms |
504 KB |
Output is correct |
30 |
Correct |
5 ms |
504 KB |
Output is correct |
31 |
Correct |
5 ms |
504 KB |
Output is correct |
32 |
Correct |
5 ms |
504 KB |
Output is correct |
33 |
Correct |
6 ms |
504 KB |
Output is correct |
34 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
380 KB |
Output is correct |
20 |
Correct |
6 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
380 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
6 ms |
504 KB |
Output is correct |
29 |
Correct |
6 ms |
504 KB |
Output is correct |
30 |
Correct |
5 ms |
504 KB |
Output is correct |
31 |
Correct |
6 ms |
504 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
33 |
Correct |
6 ms |
504 KB |
Output is correct |
34 |
Correct |
6 ms |
504 KB |
Output is correct |
35 |
Correct |
11 ms |
760 KB |
Output is correct |
36 |
Correct |
6 ms |
504 KB |
Output is correct |
37 |
Correct |
10 ms |
760 KB |
Output is correct |
38 |
Correct |
12 ms |
888 KB |
Output is correct |
39 |
Correct |
12 ms |
888 KB |
Output is correct |
40 |
Correct |
13 ms |
888 KB |
Output is correct |
41 |
Correct |
13 ms |
888 KB |
Output is correct |
42 |
Correct |
10 ms |
888 KB |
Output is correct |
43 |
Correct |
10 ms |
888 KB |
Output is correct |
44 |
Correct |
14 ms |
888 KB |
Output is correct |
45 |
Correct |
13 ms |
888 KB |
Output is correct |
46 |
Correct |
12 ms |
892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
6 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
7 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
6 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
6 ms |
504 KB |
Output is correct |
30 |
Correct |
5 ms |
504 KB |
Output is correct |
31 |
Correct |
6 ms |
504 KB |
Output is correct |
32 |
Correct |
6 ms |
504 KB |
Output is correct |
33 |
Correct |
6 ms |
504 KB |
Output is correct |
34 |
Correct |
6 ms |
508 KB |
Output is correct |
35 |
Correct |
12 ms |
888 KB |
Output is correct |
36 |
Correct |
6 ms |
504 KB |
Output is correct |
37 |
Correct |
10 ms |
760 KB |
Output is correct |
38 |
Correct |
13 ms |
888 KB |
Output is correct |
39 |
Correct |
11 ms |
888 KB |
Output is correct |
40 |
Correct |
11 ms |
888 KB |
Output is correct |
41 |
Correct |
14 ms |
888 KB |
Output is correct |
42 |
Correct |
10 ms |
888 KB |
Output is correct |
43 |
Correct |
11 ms |
888 KB |
Output is correct |
44 |
Correct |
12 ms |
888 KB |
Output is correct |
45 |
Correct |
13 ms |
888 KB |
Output is correct |
46 |
Correct |
12 ms |
888 KB |
Output is correct |
47 |
Correct |
15 ms |
1912 KB |
Output is correct |
48 |
Correct |
12 ms |
1528 KB |
Output is correct |
49 |
Correct |
12 ms |
1528 KB |
Output is correct |
50 |
Correct |
10 ms |
1528 KB |
Output is correct |
51 |
Correct |
20 ms |
2676 KB |
Output is correct |
52 |
Correct |
21 ms |
2676 KB |
Output is correct |
53 |
Correct |
13 ms |
1912 KB |
Output is correct |
54 |
Correct |
7 ms |
1404 KB |
Output is correct |
55 |
Correct |
8 ms |
1400 KB |
Output is correct |
56 |
Correct |
266 ms |
2296 KB |
Output is correct |
57 |
Correct |
7 ms |
1660 KB |
Output is correct |
58 |
Correct |
12 ms |
1656 KB |
Output is correct |
59 |
Correct |
15 ms |
1784 KB |
Output is correct |
60 |
Correct |
13 ms |
1784 KB |
Output is correct |
61 |
Correct |
15 ms |
1784 KB |
Output is correct |
62 |
Correct |
15 ms |
1912 KB |
Output is correct |
63 |
Correct |
28 ms |
2800 KB |
Output is correct |
64 |
Correct |
29 ms |
2800 KB |
Output is correct |
65 |
Correct |
32 ms |
2800 KB |
Output is correct |
66 |
Correct |
37 ms |
2676 KB |
Output is correct |
67 |
Correct |
34 ms |
2676 KB |
Output is correct |