#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
using namespace std;
const int maxn = 3e4 + 5;
int B[maxn],P[maxn];
void input(int n){
for (int i = 0 ; i < n ; i++)
cin >> B[i] >> P[i];
}
namespace sub1{
const int K = 2e3 + 5;
bool check(int n){
return (n <= K);
}
bool mark[K][K],vis[K];
int dp[K];
vector <vector<pir>> vec(100*K);
vector <vector<int>> doge(K);
void bfs(int n,int N){
vec[0].push_back({B[0],P[0]});
for (int len = 0 ; len < 100*N ; len++)
for (pir t : vec[len]){
int Bi = t.fi,Pi = t.se;
if (mark[Bi][Pi]) continue;
mark[Bi][Pi] = 1;
if (!vis[Bi])
dp[Bi] = len;
if (Bi + Pi < N)
vec[len + 1].push_back({Bi + Pi,Pi});
if (Bi - Pi >= 0)
vec[len + 1].push_back({Bi - Pi,Pi});
if (!vis[Bi])
for (int p : doge[Bi]){
if (Bi + p < N)
vec[len + 1].push_back({Bi + p,p});
if (Bi - p >= 0)
vec[len + 1].push_back({Bi - p,p});
}
vis[Bi] = 1;
}
}
int solve(int n,int N){
//dp[i][k] : position i, jump k
for (int i = 0 ; i < n ; i++)
doge[B[i]].push_back(P[i]);
bfs(n,N);
if (!vis[B[1]]) return -1;
return dp[B[1]];
}
}
namespace sub2{
const int block = sqrt(maxn);
bool vis[maxn];
bool mark[maxn][block];
int dp[maxn];
vector <vector<pir>> vec(maxn * block);
vector <vector<int>> doge(maxn);
void bfs(int n,int N){
vec[0].push_back({B[0],P[0]});
for (int len = 0 ; len < N * block ; len++)
for (pir t : vec[len]){
int Bi = t.fi,Pi = t.se;
if (Pi <= block && mark[Bi][Pi]) continue;
if (Pi <= block) mark[Bi][Pi] = 1;
if (!vis[Bi]) dp[Bi] = len;
if (!vis[Bi])
for (int p : doge[Bi])
if (p > block){
for (int x = Bi + p ; x < N ; x += p)
if (x < N && !vis[x])
vec[len + (x - Bi)/p].push_back({x,p});
for (int x = Bi - p ; x >= 0 ; x -= p)
if (x >= 0 && !vis[x])
vec[len + (Bi - x)/p].push_back({x,p});
}
if (!vis[Bi])
for (int p : doge[Bi])
if (p <= block){
if (Bi + p < N)
vec[len + 1].push_back({Bi + p,p});
if (Bi - p >= 0)
vec[len + 1].push_back({Bi - p,p});
}
if (Pi <= block && Bi + Pi < N)
vec[len + 1].push_back({Bi + Pi,Pi});
if (Pi <= block && Bi - Pi >= 0)
vec[len + 1].push_back({Bi - Pi,Pi});
vis[Bi] = 1;
}
}
int solve(int n,int N){
for (int i = 0 ; i < n ; i++)
doge[B[i]].push_back(P[i]);
bfs(n,N);
if (!vis[B[1]]) return -1;
return dp[B[1]];
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int N,n;
cin >> N >> n;
input(n);
if (sub1::check(n))
cout << sub1::solve(n,N);else
cout << sub2::solve(n,N);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
127660 KB |
Output is correct |
2 |
Correct |
54 ms |
127736 KB |
Output is correct |
3 |
Correct |
64 ms |
127824 KB |
Output is correct |
4 |
Correct |
56 ms |
127824 KB |
Output is correct |
5 |
Correct |
54 ms |
127828 KB |
Output is correct |
6 |
Correct |
53 ms |
127848 KB |
Output is correct |
7 |
Correct |
54 ms |
127824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
127604 KB |
Output is correct |
2 |
Correct |
65 ms |
127836 KB |
Output is correct |
3 |
Correct |
51 ms |
127824 KB |
Output is correct |
4 |
Correct |
54 ms |
127820 KB |
Output is correct |
5 |
Correct |
67 ms |
127824 KB |
Output is correct |
6 |
Correct |
59 ms |
127836 KB |
Output is correct |
7 |
Correct |
60 ms |
127852 KB |
Output is correct |
8 |
Correct |
50 ms |
127824 KB |
Output is correct |
9 |
Correct |
53 ms |
127824 KB |
Output is correct |
10 |
Correct |
55 ms |
127824 KB |
Output is correct |
11 |
Correct |
59 ms |
128088 KB |
Output is correct |
12 |
Correct |
58 ms |
128080 KB |
Output is correct |
13 |
Correct |
69 ms |
128080 KB |
Output is correct |
14 |
Correct |
58 ms |
128084 KB |
Output is correct |
15 |
Correct |
56 ms |
128004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
127832 KB |
Output is correct |
2 |
Correct |
54 ms |
127828 KB |
Output is correct |
3 |
Correct |
54 ms |
127824 KB |
Output is correct |
4 |
Correct |
50 ms |
127648 KB |
Output is correct |
5 |
Correct |
54 ms |
127828 KB |
Output is correct |
6 |
Correct |
55 ms |
127704 KB |
Output is correct |
7 |
Correct |
62 ms |
127836 KB |
Output is correct |
8 |
Correct |
52 ms |
127828 KB |
Output is correct |
9 |
Correct |
59 ms |
127784 KB |
Output is correct |
10 |
Correct |
56 ms |
127824 KB |
Output is correct |
11 |
Correct |
58 ms |
128060 KB |
Output is correct |
12 |
Correct |
56 ms |
128084 KB |
Output is correct |
13 |
Correct |
53 ms |
128084 KB |
Output is correct |
14 |
Correct |
73 ms |
128056 KB |
Output is correct |
15 |
Correct |
66 ms |
128084 KB |
Output is correct |
16 |
Correct |
55 ms |
127824 KB |
Output is correct |
17 |
Correct |
55 ms |
129620 KB |
Output is correct |
18 |
Correct |
53 ms |
127836 KB |
Output is correct |
19 |
Correct |
54 ms |
127824 KB |
Output is correct |
20 |
Correct |
60 ms |
131920 KB |
Output is correct |
21 |
Correct |
59 ms |
127824 KB |
Output is correct |
22 |
Correct |
54 ms |
127664 KB |
Output is correct |
23 |
Correct |
59 ms |
130908 KB |
Output is correct |
24 |
Correct |
57 ms |
131668 KB |
Output is correct |
25 |
Correct |
68 ms |
131920 KB |
Output is correct |
26 |
Correct |
59 ms |
131940 KB |
Output is correct |
27 |
Correct |
56 ms |
131920 KB |
Output is correct |
28 |
Correct |
55 ms |
132432 KB |
Output is correct |
29 |
Correct |
55 ms |
132800 KB |
Output is correct |
30 |
Correct |
54 ms |
131924 KB |
Output is correct |
31 |
Correct |
55 ms |
132444 KB |
Output is correct |
32 |
Correct |
56 ms |
132176 KB |
Output is correct |
33 |
Correct |
59 ms |
133972 KB |
Output is correct |
34 |
Correct |
56 ms |
133968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
127836 KB |
Output is correct |
2 |
Correct |
56 ms |
127732 KB |
Output is correct |
3 |
Correct |
54 ms |
127660 KB |
Output is correct |
4 |
Correct |
54 ms |
127820 KB |
Output is correct |
5 |
Correct |
53 ms |
127836 KB |
Output is correct |
6 |
Correct |
55 ms |
127824 KB |
Output is correct |
7 |
Correct |
59 ms |
127824 KB |
Output is correct |
8 |
Correct |
56 ms |
127732 KB |
Output is correct |
9 |
Correct |
54 ms |
127828 KB |
Output is correct |
10 |
Correct |
62 ms |
127828 KB |
Output is correct |
11 |
Correct |
57 ms |
128076 KB |
Output is correct |
12 |
Correct |
54 ms |
127928 KB |
Output is correct |
13 |
Correct |
55 ms |
128092 KB |
Output is correct |
14 |
Correct |
53 ms |
128080 KB |
Output is correct |
15 |
Correct |
63 ms |
127996 KB |
Output is correct |
16 |
Correct |
55 ms |
127828 KB |
Output is correct |
17 |
Correct |
56 ms |
129620 KB |
Output is correct |
18 |
Correct |
56 ms |
127824 KB |
Output is correct |
19 |
Correct |
55 ms |
127828 KB |
Output is correct |
20 |
Correct |
59 ms |
131920 KB |
Output is correct |
21 |
Correct |
55 ms |
127828 KB |
Output is correct |
22 |
Correct |
69 ms |
127824 KB |
Output is correct |
23 |
Correct |
51 ms |
131152 KB |
Output is correct |
24 |
Correct |
55 ms |
131660 KB |
Output is correct |
25 |
Correct |
58 ms |
131936 KB |
Output is correct |
26 |
Correct |
56 ms |
131924 KB |
Output is correct |
27 |
Correct |
62 ms |
132052 KB |
Output is correct |
28 |
Correct |
58 ms |
132436 KB |
Output is correct |
29 |
Correct |
58 ms |
132812 KB |
Output is correct |
30 |
Correct |
57 ms |
131920 KB |
Output is correct |
31 |
Correct |
69 ms |
132392 KB |
Output is correct |
32 |
Correct |
57 ms |
132176 KB |
Output is correct |
33 |
Correct |
55 ms |
133980 KB |
Output is correct |
34 |
Correct |
59 ms |
133972 KB |
Output is correct |
35 |
Correct |
55 ms |
129668 KB |
Output is correct |
36 |
Correct |
58 ms |
128336 KB |
Output is correct |
37 |
Correct |
63 ms |
130524 KB |
Output is correct |
38 |
Correct |
61 ms |
130640 KB |
Output is correct |
39 |
Correct |
58 ms |
130640 KB |
Output is correct |
40 |
Correct |
70 ms |
130560 KB |
Output is correct |
41 |
Correct |
76 ms |
130564 KB |
Output is correct |
42 |
Correct |
70 ms |
129488 KB |
Output is correct |
43 |
Correct |
60 ms |
129504 KB |
Output is correct |
44 |
Correct |
62 ms |
129484 KB |
Output is correct |
45 |
Correct |
66 ms |
135192 KB |
Output is correct |
46 |
Correct |
67 ms |
135100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
127824 KB |
Output is correct |
2 |
Correct |
54 ms |
127828 KB |
Output is correct |
3 |
Correct |
67 ms |
127824 KB |
Output is correct |
4 |
Correct |
49 ms |
127824 KB |
Output is correct |
5 |
Correct |
53 ms |
127824 KB |
Output is correct |
6 |
Correct |
58 ms |
127824 KB |
Output is correct |
7 |
Correct |
55 ms |
127840 KB |
Output is correct |
8 |
Correct |
53 ms |
127828 KB |
Output is correct |
9 |
Correct |
53 ms |
127840 KB |
Output is correct |
10 |
Correct |
52 ms |
127840 KB |
Output is correct |
11 |
Correct |
51 ms |
128080 KB |
Output is correct |
12 |
Correct |
53 ms |
128076 KB |
Output is correct |
13 |
Correct |
55 ms |
128084 KB |
Output is correct |
14 |
Correct |
56 ms |
128084 KB |
Output is correct |
15 |
Correct |
53 ms |
128096 KB |
Output is correct |
16 |
Correct |
53 ms |
127824 KB |
Output is correct |
17 |
Correct |
58 ms |
129424 KB |
Output is correct |
18 |
Correct |
57 ms |
127832 KB |
Output is correct |
19 |
Correct |
56 ms |
127828 KB |
Output is correct |
20 |
Correct |
53 ms |
131920 KB |
Output is correct |
21 |
Correct |
55 ms |
127828 KB |
Output is correct |
22 |
Correct |
66 ms |
127772 KB |
Output is correct |
23 |
Correct |
56 ms |
131080 KB |
Output is correct |
24 |
Correct |
61 ms |
131668 KB |
Output is correct |
25 |
Correct |
58 ms |
131920 KB |
Output is correct |
26 |
Correct |
58 ms |
131924 KB |
Output is correct |
27 |
Correct |
55 ms |
131920 KB |
Output is correct |
28 |
Correct |
54 ms |
132340 KB |
Output is correct |
29 |
Correct |
56 ms |
132944 KB |
Output is correct |
30 |
Correct |
55 ms |
131920 KB |
Output is correct |
31 |
Correct |
50 ms |
132432 KB |
Output is correct |
32 |
Correct |
55 ms |
132176 KB |
Output is correct |
33 |
Correct |
54 ms |
133984 KB |
Output is correct |
34 |
Correct |
82 ms |
133972 KB |
Output is correct |
35 |
Correct |
75 ms |
129876 KB |
Output is correct |
36 |
Correct |
51 ms |
128180 KB |
Output is correct |
37 |
Correct |
63 ms |
130636 KB |
Output is correct |
38 |
Correct |
70 ms |
130660 KB |
Output is correct |
39 |
Correct |
65 ms |
130644 KB |
Output is correct |
40 |
Correct |
68 ms |
130496 KB |
Output is correct |
41 |
Correct |
62 ms |
130648 KB |
Output is correct |
42 |
Correct |
52 ms |
129492 KB |
Output is correct |
43 |
Correct |
59 ms |
129488 KB |
Output is correct |
44 |
Correct |
56 ms |
129488 KB |
Output is correct |
45 |
Correct |
86 ms |
135288 KB |
Output is correct |
46 |
Correct |
66 ms |
135192 KB |
Output is correct |
47 |
Correct |
73 ms |
139808 KB |
Output is correct |
48 |
Correct |
64 ms |
128592 KB |
Output is correct |
49 |
Correct |
72 ms |
128472 KB |
Output is correct |
50 |
Correct |
66 ms |
128472 KB |
Output is correct |
51 |
Correct |
88 ms |
138548 KB |
Output is correct |
52 |
Correct |
85 ms |
138992 KB |
Output is correct |
53 |
Correct |
70 ms |
135548 KB |
Output is correct |
54 |
Runtime error |
667 ms |
262148 KB |
Execution killed with signal 9 |
55 |
Halted |
0 ms |
0 KB |
- |