/* made by amunduzbaev */
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define tut(x) array<int, x>
//#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b? a = b, true:false; }
template<class T> bool umax(T& a, const T& b) { return a < b? a = b, true:false; }
//void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
//freopen((s+".out").c_str(),"w",stdout); }
//template<class T> tree<T,
//less<T>, null_type, rb_tree_tag,
//tree_order_statistics_node_update> ordered_set;
const int N = 3e4+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
const int M = 2e3+5;
const int B = 500;
#define MULTI 0
int n, m, K, s, ans, q, res, a[N];
bitset<N> used, vis[N];
int b[N], p[N];
vii cnt[N];
void solve(int t_case){
cin>>m>>n;
for(int i=0;i<n;i++) cin>>b[i]>>p[i], cnt[b[i]].pb(p[i]);
deque<tut(3)> qq;
qq.pb({b[0], p[0], 0});
vis[b[0]][p[0]] = 1;
while(!qq.empty()){
auto cur = qq.front(); qq.pop_front();
if(cur[0] == b[1]) { cout<<cur[2]<<"\n"; return; }
if(cur[0] + cur[1] < m && vis[cur[0] + cur[1]][cur[1]] == 0) vis[cur[0] + cur[1]][cur[1]] = 1, qq.pb({cur[0] + cur[1], cur[1], cur[2] + 1});
if(cur[0] - cur[1] >= 0 && vis[cur[0] - cur[1]][cur[1]] == 0) vis[cur[0] - cur[1]][cur[1]] = 1, qq.pb({cur[0] - cur[1], cur[1], cur[2] + 1});
if(!used[cur[0]]){
used[cur[0]] = 1;
for(auto x : cnt[cur[0]]){
if(!vis[cur[0]][x]) vis[cur[0]][x] = 1, qq.push_front({cur[0], x, cur[2]});
}
}
} cout<<-1<<"\n";
}
signed main(){
NeedForSpeed
if(!MULTI) {
solve(1);
} else {
int t; cin>>t;
for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
} 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 |
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 |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1232 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 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 |
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 |
3 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
3660 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
8 ms |
8396 KB |
Output is correct |
21 |
Correct |
2 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
6348 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
2 ms |
2512 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
7 ms |
7572 KB |
Output is correct |
28 |
Correct |
8 ms |
8396 KB |
Output is correct |
29 |
Correct |
8 ms |
8368 KB |
Output is correct |
30 |
Correct |
6 ms |
8288 KB |
Output is correct |
31 |
Correct |
7 ms |
8396 KB |
Output is correct |
32 |
Correct |
7 ms |
8268 KB |
Output is correct |
33 |
Correct |
10 ms |
8456 KB |
Output is correct |
34 |
Correct |
8 ms |
8396 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 |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1360 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
3716 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1104 KB |
Output is correct |
20 |
Correct |
6 ms |
8396 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
6356 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
3 ms |
2512 KB |
Output is correct |
26 |
Correct |
6 ms |
7244 KB |
Output is correct |
27 |
Correct |
6 ms |
7628 KB |
Output is correct |
28 |
Correct |
7 ms |
8396 KB |
Output is correct |
29 |
Correct |
7 ms |
8396 KB |
Output is correct |
30 |
Correct |
6 ms |
8268 KB |
Output is correct |
31 |
Correct |
8 ms |
8396 KB |
Output is correct |
32 |
Correct |
6 ms |
8268 KB |
Output is correct |
33 |
Correct |
10 ms |
8436 KB |
Output is correct |
34 |
Correct |
9 ms |
8396 KB |
Output is correct |
35 |
Correct |
13 ms |
6800 KB |
Output is correct |
36 |
Correct |
4 ms |
3404 KB |
Output is correct |
37 |
Correct |
11 ms |
8680 KB |
Output is correct |
38 |
Correct |
14 ms |
8872 KB |
Output is correct |
39 |
Correct |
8 ms |
2168 KB |
Output is correct |
40 |
Correct |
15 ms |
7756 KB |
Output is correct |
41 |
Correct |
13 ms |
8496 KB |
Output is correct |
42 |
Correct |
10 ms |
7788 KB |
Output is correct |
43 |
Correct |
10 ms |
8080 KB |
Output is correct |
44 |
Correct |
11 ms |
8784 KB |
Output is correct |
45 |
Correct |
23 ms |
9144 KB |
Output is correct |
46 |
Correct |
17 ms |
9284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
2 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 |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
3788 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
8 ms |
8396 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
6296 KB |
Output is correct |
24 |
Correct |
6 ms |
7500 KB |
Output is correct |
25 |
Correct |
3 ms |
2508 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
6 ms |
7628 KB |
Output is correct |
28 |
Correct |
7 ms |
8396 KB |
Output is correct |
29 |
Correct |
7 ms |
8396 KB |
Output is correct |
30 |
Correct |
6 ms |
8268 KB |
Output is correct |
31 |
Correct |
7 ms |
8268 KB |
Output is correct |
32 |
Correct |
7 ms |
8284 KB |
Output is correct |
33 |
Correct |
9 ms |
8396 KB |
Output is correct |
34 |
Correct |
8 ms |
8396 KB |
Output is correct |
35 |
Correct |
14 ms |
6860 KB |
Output is correct |
36 |
Correct |
4 ms |
3404 KB |
Output is correct |
37 |
Correct |
11 ms |
8652 KB |
Output is correct |
38 |
Correct |
14 ms |
8912 KB |
Output is correct |
39 |
Correct |
13 ms |
2252 KB |
Output is correct |
40 |
Correct |
12 ms |
7756 KB |
Output is correct |
41 |
Correct |
13 ms |
8584 KB |
Output is correct |
42 |
Correct |
10 ms |
7712 KB |
Output is correct |
43 |
Correct |
11 ms |
8012 KB |
Output is correct |
44 |
Correct |
15 ms |
8764 KB |
Output is correct |
45 |
Correct |
26 ms |
9156 KB |
Output is correct |
46 |
Correct |
17 ms |
9064 KB |
Output is correct |
47 |
Correct |
24 ms |
26780 KB |
Output is correct |
48 |
Correct |
7 ms |
1612 KB |
Output is correct |
49 |
Correct |
7 ms |
1612 KB |
Output is correct |
50 |
Correct |
5 ms |
1484 KB |
Output is correct |
51 |
Correct |
72 ms |
88388 KB |
Output is correct |
52 |
Correct |
77 ms |
94768 KB |
Output is correct |
53 |
Correct |
23 ms |
18636 KB |
Output is correct |
54 |
Correct |
38 ms |
61208 KB |
Output is correct |
55 |
Correct |
58 ms |
77808 KB |
Output is correct |
56 |
Correct |
76 ms |
112208 KB |
Output is correct |
57 |
Correct |
3 ms |
2636 KB |
Output is correct |
58 |
Correct |
79 ms |
111548 KB |
Output is correct |
59 |
Correct |
84 ms |
103748 KB |
Output is correct |
60 |
Correct |
80 ms |
103812 KB |
Output is correct |
61 |
Correct |
73 ms |
98760 KB |
Output is correct |
62 |
Correct |
122 ms |
112324 KB |
Output is correct |
63 |
Correct |
200 ms |
111744 KB |
Output is correct |
64 |
Correct |
208 ms |
111800 KB |
Output is correct |
65 |
Correct |
298 ms |
111940 KB |
Output is correct |
66 |
Correct |
431 ms |
112188 KB |
Output is correct |
67 |
Correct |
249 ms |
111940 KB |
Output is correct |