#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 ll inf = 1e15 + 5;
const int mxN = 30005;
int n, m;
int B[mxN];
int P[mxN];
vector<pii> g[mxN];
ll dist[mxN];
map<int,int> mp[mxN];
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){
int pos = B[i] + P[i], cost = 1;
while(pos < m){
g[B[i]].pb({pos, cost}); cost += 1;
if(mp[pos].count(P[i]) == 1)break;
mp[pos][P[i]] = 1;
pos += P[i];
}
}
ff(i,0,m - 1)mp[i].clear();
ff(i,0,n - 1){
int pos = B[i] - P[i], cost = 1;
while(pos >= 0){
g[B[i]].pb({pos, cost}); cost += 1;
if(mp[pos].count(P[i]) == 1)break;
mp[pos][P[i]] = 1;
pos -= P[i];
}
}
ff(i,0,m - 1)dist[i] = inf;
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({dist[S] = 0, S});
while(sz(pq)){
pll 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;
}
/*
5 3
0 2
1 1
4 1
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2452 KB |
Output is correct |
2 |
Correct |
2 ms |
2448 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2448 KB |
Output is correct |
5 |
Correct |
1 ms |
2412 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2448 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2452 KB |
Output is correct |
5 |
Correct |
2 ms |
2364 KB |
Output is correct |
6 |
Correct |
2 ms |
2448 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
4 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2444 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2444 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2456 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2476 KB |
Output is correct |
17 |
Correct |
4 ms |
3028 KB |
Output is correct |
18 |
Correct |
3 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2584 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2460 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
3 ms |
2644 KB |
Output is correct |
24 |
Correct |
4 ms |
2900 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
3 ms |
2900 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
5 ms |
3156 KB |
Output is correct |
29 |
Correct |
15 ms |
6100 KB |
Output is correct |
30 |
Correct |
5 ms |
3584 KB |
Output is correct |
31 |
Correct |
9 ms |
4500 KB |
Output is correct |
32 |
Correct |
6 ms |
3864 KB |
Output is correct |
33 |
Correct |
30 ms |
9272 KB |
Output is correct |
34 |
Correct |
35 ms |
9352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2408 KB |
Output is correct |
5 |
Correct |
2 ms |
2452 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2444 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2648 KB |
Output is correct |
16 |
Correct |
3 ms |
2672 KB |
Output is correct |
17 |
Correct |
6 ms |
2976 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2580 KB |
Output is correct |
20 |
Correct |
3 ms |
2724 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
3 ms |
2644 KB |
Output is correct |
24 |
Correct |
6 ms |
2972 KB |
Output is correct |
25 |
Correct |
3 ms |
2840 KB |
Output is correct |
26 |
Correct |
3 ms |
2900 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
5 ms |
3156 KB |
Output is correct |
29 |
Correct |
17 ms |
6084 KB |
Output is correct |
30 |
Correct |
5 ms |
3540 KB |
Output is correct |
31 |
Correct |
8 ms |
4496 KB |
Output is correct |
32 |
Correct |
6 ms |
3796 KB |
Output is correct |
33 |
Correct |
46 ms |
9276 KB |
Output is correct |
34 |
Correct |
40 ms |
9292 KB |
Output is correct |
35 |
Correct |
25 ms |
5964 KB |
Output is correct |
36 |
Correct |
5 ms |
3028 KB |
Output is correct |
37 |
Correct |
48 ms |
8956 KB |
Output is correct |
38 |
Correct |
40 ms |
8168 KB |
Output is correct |
39 |
Correct |
48 ms |
8268 KB |
Output is correct |
40 |
Correct |
47 ms |
8268 KB |
Output is correct |
41 |
Correct |
52 ms |
8176 KB |
Output is correct |
42 |
Correct |
9 ms |
3792 KB |
Output is correct |
43 |
Correct |
8 ms |
3688 KB |
Output is correct |
44 |
Correct |
8 ms |
3640 KB |
Output is correct |
45 |
Correct |
282 ms |
28332 KB |
Output is correct |
46 |
Correct |
294 ms |
28336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2448 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2444 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2712 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
4 ms |
3028 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2580 KB |
Output is correct |
23 |
Correct |
3 ms |
2644 KB |
Output is correct |
24 |
Correct |
4 ms |
2900 KB |
Output is correct |
25 |
Correct |
3 ms |
2772 KB |
Output is correct |
26 |
Correct |
3 ms |
2900 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
4 ms |
3156 KB |
Output is correct |
29 |
Correct |
16 ms |
6168 KB |
Output is correct |
30 |
Correct |
5 ms |
3540 KB |
Output is correct |
31 |
Correct |
11 ms |
4448 KB |
Output is correct |
32 |
Correct |
7 ms |
3796 KB |
Output is correct |
33 |
Correct |
43 ms |
9316 KB |
Output is correct |
34 |
Correct |
36 ms |
9372 KB |
Output is correct |
35 |
Correct |
25 ms |
5912 KB |
Output is correct |
36 |
Correct |
5 ms |
3028 KB |
Output is correct |
37 |
Correct |
47 ms |
9036 KB |
Output is correct |
38 |
Correct |
43 ms |
8172 KB |
Output is correct |
39 |
Correct |
50 ms |
8268 KB |
Output is correct |
40 |
Correct |
48 ms |
8208 KB |
Output is correct |
41 |
Correct |
45 ms |
8268 KB |
Output is correct |
42 |
Correct |
8 ms |
3792 KB |
Output is correct |
43 |
Correct |
9 ms |
3676 KB |
Output is correct |
44 |
Correct |
8 ms |
3576 KB |
Output is correct |
45 |
Correct |
294 ms |
28252 KB |
Output is correct |
46 |
Correct |
272 ms |
28336 KB |
Output is correct |
47 |
Correct |
358 ms |
30512 KB |
Output is correct |
48 |
Correct |
61 ms |
10560 KB |
Output is correct |
49 |
Correct |
34 ms |
7168 KB |
Output is correct |
50 |
Correct |
29 ms |
7468 KB |
Output is correct |
51 |
Correct |
92 ms |
14856 KB |
Output is correct |
52 |
Correct |
138 ms |
16232 KB |
Output is correct |
53 |
Correct |
25 ms |
7472 KB |
Output is correct |
54 |
Correct |
9 ms |
4316 KB |
Output is correct |
55 |
Correct |
10 ms |
5816 KB |
Output is correct |
56 |
Correct |
22 ms |
6740 KB |
Output is correct |
57 |
Incorrect |
249 ms |
30172 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |