#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for (auto& a : x)
#define sz(a) (int)(a).size()
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 30005;
int n, m;
int B[mxN];
int P[mxN];
vector<pii> g[mxN];
int dist[mxN];
unordered_map<int,int> L[mxN];
unordered_map<int,int> R[mxN];
int id[mxN];
bool cmp(int s1, int s2){
return B[s1] < B[s2];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> m >> n;
ff(i,0,n - 1)cin >> B[i] >> P[i];
int S = B[0];
int T = B[1];
ff(i,0,n - 1)id[i] = i;
sort(id, id + n, cmp);
ff(i,0,n - 1){
int pos = B[id[i]] + P[id[i]], cost = 1;
while(pos < m){
g[B[id[i]]].pb({pos, cost}); cost += 1;
if(L[pos].count(P[id[i]]) == 1)break;
L[pos][P[id[i]]] = 1;
pos += P[id[i]];
}
}
ff(i,0,n - 1){
int pos = B[id[i]] - P[id[i]], cost = 1;
while(pos >= 0){
g[B[id[i]]].pb({pos, cost}); cost += 1;
if(R[pos].count(P[id[i]]) == 1)break;
R[pos][P[id[i]]] = 1;
pos -= P[id[i]];
}
}
ff(i,0,m - 1)dist[i] = inf;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({dist[S] = 0, S});
while(sz(pq)){
pii v = pq.top(); pq.pop();
if(dist[v.se] < v.fi)continue;
for(auto c : g[v.se]){
int u = c.fi;
int w = c.se;
if(dist[u] > v.fi + w){
pq.push({dist[u] = v.fi + w, u});
}
}
}
cout << (dist[T] == inf ? -1 : dist[T]) << '\n';
return 0;
}
/*
6 4
4 5
0 3
5 1
4 1
18 10
1 2
0 12
1 17
4 1
3 1
5 13
14 7
5 16
15 6
9 5
51 9
9 1
45 2
42 38
17 9
39 44
35 1
22 41
43 35
3 36
22 19
10 10
13 5
1 1
17 17
2 3
3 9
20 22
18 1
3 5
11 16
7 16
2 22
20 4
11 19
7 1
16 1
11 14
8 9
3 18
5 3
0 2
1 1
4 1
// probati bojenje sahovski
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4308 KB |
Output is correct |
2 |
Correct |
2 ms |
4308 KB |
Output is correct |
3 |
Correct |
2 ms |
4308 KB |
Output is correct |
4 |
Correct |
2 ms |
4308 KB |
Output is correct |
5 |
Correct |
2 ms |
4308 KB |
Output is correct |
6 |
Correct |
2 ms |
4308 KB |
Output is correct |
7 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
3 |
Correct |
2 ms |
4308 KB |
Output is correct |
4 |
Correct |
2 ms |
4308 KB |
Output is correct |
5 |
Correct |
2 ms |
4336 KB |
Output is correct |
6 |
Correct |
2 ms |
4308 KB |
Output is correct |
7 |
Correct |
3 ms |
4308 KB |
Output is correct |
8 |
Correct |
2 ms |
4308 KB |
Output is correct |
9 |
Correct |
2 ms |
4308 KB |
Output is correct |
10 |
Correct |
3 ms |
4308 KB |
Output is correct |
11 |
Correct |
3 ms |
4564 KB |
Output is correct |
12 |
Correct |
3 ms |
4436 KB |
Output is correct |
13 |
Correct |
2 ms |
4436 KB |
Output is correct |
14 |
Correct |
3 ms |
4564 KB |
Output is correct |
15 |
Correct |
4 ms |
4564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
3 |
Correct |
3 ms |
4308 KB |
Output is correct |
4 |
Correct |
2 ms |
4308 KB |
Output is correct |
5 |
Correct |
4 ms |
4308 KB |
Output is correct |
6 |
Correct |
3 ms |
4308 KB |
Output is correct |
7 |
Correct |
2 ms |
4308 KB |
Output is correct |
8 |
Correct |
2 ms |
4308 KB |
Output is correct |
9 |
Correct |
3 ms |
4276 KB |
Output is correct |
10 |
Correct |
2 ms |
4308 KB |
Output is correct |
11 |
Correct |
3 ms |
4564 KB |
Output is correct |
12 |
Correct |
3 ms |
4436 KB |
Output is correct |
13 |
Correct |
3 ms |
4436 KB |
Output is correct |
14 |
Correct |
3 ms |
4564 KB |
Output is correct |
15 |
Correct |
4 ms |
4600 KB |
Output is correct |
16 |
Correct |
4 ms |
4436 KB |
Output is correct |
17 |
Correct |
6 ms |
5108 KB |
Output is correct |
18 |
Correct |
5 ms |
4820 KB |
Output is correct |
19 |
Correct |
3 ms |
4692 KB |
Output is correct |
20 |
Correct |
4 ms |
4980 KB |
Output is correct |
21 |
Correct |
3 ms |
4436 KB |
Output is correct |
22 |
Correct |
4 ms |
4820 KB |
Output is correct |
23 |
Correct |
4 ms |
4820 KB |
Output is correct |
24 |
Correct |
5 ms |
5204 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
4948 KB |
Output is correct |
28 |
Correct |
6 ms |
5588 KB |
Output is correct |
29 |
Correct |
17 ms |
8148 KB |
Output is correct |
30 |
Correct |
7 ms |
5588 KB |
Output is correct |
31 |
Correct |
10 ms |
6488 KB |
Output is correct |
32 |
Correct |
6 ms |
5844 KB |
Output is correct |
33 |
Correct |
32 ms |
11316 KB |
Output is correct |
34 |
Correct |
30 ms |
11252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
3 |
Correct |
3 ms |
4264 KB |
Output is correct |
4 |
Correct |
3 ms |
4308 KB |
Output is correct |
5 |
Correct |
3 ms |
4308 KB |
Output is correct |
6 |
Correct |
3 ms |
4308 KB |
Output is correct |
7 |
Correct |
2 ms |
4308 KB |
Output is correct |
8 |
Correct |
2 ms |
4308 KB |
Output is correct |
9 |
Correct |
3 ms |
4308 KB |
Output is correct |
10 |
Correct |
3 ms |
4308 KB |
Output is correct |
11 |
Correct |
5 ms |
4564 KB |
Output is correct |
12 |
Correct |
3 ms |
4436 KB |
Output is correct |
13 |
Correct |
3 ms |
4436 KB |
Output is correct |
14 |
Correct |
3 ms |
4564 KB |
Output is correct |
15 |
Correct |
3 ms |
4588 KB |
Output is correct |
16 |
Correct |
3 ms |
4436 KB |
Output is correct |
17 |
Correct |
5 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
4876 KB |
Output is correct |
19 |
Correct |
3 ms |
4692 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
4436 KB |
Output is correct |
22 |
Correct |
3 ms |
4820 KB |
Output is correct |
23 |
Correct |
4 ms |
4820 KB |
Output is correct |
24 |
Correct |
5 ms |
5204 KB |
Output is correct |
25 |
Correct |
5 ms |
5192 KB |
Output is correct |
26 |
Correct |
5 ms |
5204 KB |
Output is correct |
27 |
Correct |
5 ms |
4948 KB |
Output is correct |
28 |
Correct |
6 ms |
5588 KB |
Output is correct |
29 |
Correct |
17 ms |
8100 KB |
Output is correct |
30 |
Correct |
6 ms |
5588 KB |
Output is correct |
31 |
Correct |
9 ms |
6480 KB |
Output is correct |
32 |
Correct |
7 ms |
5844 KB |
Output is correct |
33 |
Correct |
31 ms |
11260 KB |
Output is correct |
34 |
Correct |
27 ms |
11352 KB |
Output is correct |
35 |
Correct |
23 ms |
9356 KB |
Output is correct |
36 |
Correct |
5 ms |
5204 KB |
Output is correct |
37 |
Correct |
49 ms |
14180 KB |
Output is correct |
38 |
Correct |
49 ms |
12832 KB |
Output is correct |
39 |
Correct |
45 ms |
12884 KB |
Output is correct |
40 |
Correct |
45 ms |
12876 KB |
Output is correct |
41 |
Correct |
57 ms |
12876 KB |
Output is correct |
42 |
Correct |
10 ms |
5928 KB |
Output is correct |
43 |
Correct |
9 ms |
5724 KB |
Output is correct |
44 |
Correct |
13 ms |
5840 KB |
Output is correct |
45 |
Correct |
212 ms |
27196 KB |
Output is correct |
46 |
Correct |
178 ms |
27304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4308 KB |
Output is correct |
3 |
Correct |
2 ms |
4308 KB |
Output is correct |
4 |
Correct |
4 ms |
4308 KB |
Output is correct |
5 |
Correct |
5 ms |
4352 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
3 ms |
4308 KB |
Output is correct |
8 |
Correct |
3 ms |
4308 KB |
Output is correct |
9 |
Correct |
2 ms |
4308 KB |
Output is correct |
10 |
Correct |
3 ms |
4308 KB |
Output is correct |
11 |
Correct |
4 ms |
4564 KB |
Output is correct |
12 |
Correct |
3 ms |
4436 KB |
Output is correct |
13 |
Correct |
3 ms |
4436 KB |
Output is correct |
14 |
Correct |
3 ms |
4564 KB |
Output is correct |
15 |
Correct |
4 ms |
4564 KB |
Output is correct |
16 |
Correct |
5 ms |
4556 KB |
Output is correct |
17 |
Correct |
8 ms |
5208 KB |
Output is correct |
18 |
Correct |
5 ms |
4820 KB |
Output is correct |
19 |
Correct |
4 ms |
4692 KB |
Output is correct |
20 |
Correct |
5 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
4436 KB |
Output is correct |
22 |
Correct |
6 ms |
4820 KB |
Output is correct |
23 |
Correct |
5 ms |
4820 KB |
Output is correct |
24 |
Correct |
5 ms |
5204 KB |
Output is correct |
25 |
Correct |
5 ms |
5076 KB |
Output is correct |
26 |
Correct |
6 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
4948 KB |
Output is correct |
28 |
Correct |
6 ms |
5588 KB |
Output is correct |
29 |
Correct |
18 ms |
8120 KB |
Output is correct |
30 |
Correct |
6 ms |
5588 KB |
Output is correct |
31 |
Correct |
13 ms |
6528 KB |
Output is correct |
32 |
Correct |
7 ms |
5844 KB |
Output is correct |
33 |
Correct |
50 ms |
11308 KB |
Output is correct |
34 |
Correct |
34 ms |
11348 KB |
Output is correct |
35 |
Correct |
33 ms |
9332 KB |
Output is correct |
36 |
Correct |
7 ms |
5204 KB |
Output is correct |
37 |
Correct |
45 ms |
14224 KB |
Output is correct |
38 |
Correct |
49 ms |
12832 KB |
Output is correct |
39 |
Correct |
57 ms |
12920 KB |
Output is correct |
40 |
Correct |
56 ms |
12876 KB |
Output is correct |
41 |
Correct |
57 ms |
12880 KB |
Output is correct |
42 |
Correct |
9 ms |
5960 KB |
Output is correct |
43 |
Correct |
10 ms |
5724 KB |
Output is correct |
44 |
Correct |
9 ms |
5828 KB |
Output is correct |
45 |
Correct |
241 ms |
27232 KB |
Output is correct |
46 |
Correct |
275 ms |
27308 KB |
Output is correct |
47 |
Correct |
414 ms |
46852 KB |
Output is correct |
48 |
Correct |
78 ms |
18248 KB |
Output is correct |
49 |
Correct |
51 ms |
14248 KB |
Output is correct |
50 |
Correct |
45 ms |
14708 KB |
Output is correct |
51 |
Correct |
116 ms |
24364 KB |
Output is correct |
52 |
Correct |
158 ms |
26680 KB |
Output is correct |
53 |
Correct |
34 ms |
15564 KB |
Output is correct |
54 |
Correct |
11 ms |
9152 KB |
Output is correct |
55 |
Correct |
23 ms |
14532 KB |
Output is correct |
56 |
Correct |
37 ms |
15340 KB |
Output is correct |
57 |
Correct |
290 ms |
46760 KB |
Output is correct |
58 |
Correct |
31 ms |
15748 KB |
Output is correct |
59 |
Correct |
41 ms |
16096 KB |
Output is correct |
60 |
Correct |
56 ms |
19308 KB |
Output is correct |
61 |
Correct |
45 ms |
15348 KB |
Output is correct |
62 |
Correct |
172 ms |
31108 KB |
Output is correct |
63 |
Execution timed out |
1102 ms |
102720 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |