//Never stop trying
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef string str;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
const int MOD = 1e9 + 7; //998244353
const ll INF = 2e9;
const int MX = 3e4 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
ll random(ll a, ll b){
return a + rng() % (b - a + 1);
}
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl;
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/////////////////////////ONLY CLEAN CODES ALLOWED/////////////////////////
const int mxP=40;
int N,M;
vi pos(MX),vec[MX],P(MX);
V<pair<pi,int>> adj[MX][mxP+1];
int main() {
boost; IO();
cin>>N>>M;
FOR(i,0,M){
cin>>pos[i]>>P[i];
if(P[i]<=mxP) vec[pos[i]].pb(P[i]);
}
FOR(i,0,N){
FOR(j,0,mxP+1){
if(i+j<N){
adj[i][j].pb({{i+j,j},1});
adj[i][j].pb({{i+j,0},1});
}
if(i-j>=0){
adj[i][j].pb({{i-j,j},1});
adj[i][j].pb({{i-j,0},1});
}
}
for(auto j: vec[i]) adj[i][0].pb({{i,j},0});
}
FOR(i,0,M) if(P[i]>mxP){
int x=pos[i],jump=0;
while(x+P[i]*(jump+1)<N){
jump++;
adj[x][0].pb({{x+P[i]*jump,0},jump});
}
jump=0;
while(x-P[i]*(jump+1)>=0){
jump++;
adj[x][0].pb({{x-P[i]*jump,0},jump});
}
}
priority_queue<pair<int,pi>,V<pair<int,pi>>,greater<pair<int,pi>>>q;
q.push({0,{pos[0],0}});
int dist[N][mxP+1]; FOR(i,0,N) FOR(j,0,mxP+1) dist[i][j]=INF;
dist[pos[0]][0]=0;
while(!q.empty()){
int u=q.top().se.fi,p=q.top().se.se,d=q.top().fi;
q.pop();
if(dist[u][p]<d) continue;
for(auto it: adj[u][p]){
int v=it.fi.fi,pp=it.fi.se,w=it.se;
if(dist[v][pp]>d+w){
dist[v][pp]=d+w;
q.push({dist[v][pp],{v,pp}});
}
}
}
int ans=dist[pos[1]][0];
if(ans==INF) ans=-1;
cout << ans << endl;
return 0;
}
/*
(pos,power)-->(pos+-power,power) (<=sqrt(N))
(pos,power)-->(pos+-power,0) (<=sqrt(N))
(pos,0)--> (pos,power) (all powers in that pos) (<=sqrt(N))
(pos,0)-->(nw_pos,0) (>sqrt(N))
*/
//Change your approach
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
30188 KB |
Output is correct |
2 |
Correct |
21 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30188 KB |
Output is correct |
7 |
Correct |
21 ms |
30188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
30188 KB |
Output is correct |
2 |
Correct |
20 ms |
30136 KB |
Output is correct |
3 |
Correct |
23 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30188 KB |
Output is correct |
7 |
Correct |
21 ms |
30188 KB |
Output is correct |
8 |
Correct |
22 ms |
30188 KB |
Output is correct |
9 |
Correct |
21 ms |
30316 KB |
Output is correct |
10 |
Correct |
21 ms |
30444 KB |
Output is correct |
11 |
Correct |
22 ms |
30444 KB |
Output is correct |
12 |
Correct |
21 ms |
30444 KB |
Output is correct |
13 |
Correct |
22 ms |
30444 KB |
Output is correct |
14 |
Correct |
22 ms |
30444 KB |
Output is correct |
15 |
Correct |
22 ms |
30572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
30188 KB |
Output is correct |
2 |
Correct |
20 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30188 KB |
Output is correct |
7 |
Correct |
21 ms |
30188 KB |
Output is correct |
8 |
Correct |
21 ms |
30188 KB |
Output is correct |
9 |
Correct |
22 ms |
30316 KB |
Output is correct |
10 |
Correct |
21 ms |
30444 KB |
Output is correct |
11 |
Correct |
22 ms |
30444 KB |
Output is correct |
12 |
Correct |
22 ms |
30444 KB |
Output is correct |
13 |
Correct |
21 ms |
30444 KB |
Output is correct |
14 |
Correct |
22 ms |
30444 KB |
Output is correct |
15 |
Correct |
22 ms |
30572 KB |
Output is correct |
16 |
Correct |
22 ms |
30700 KB |
Output is correct |
17 |
Correct |
27 ms |
32364 KB |
Output is correct |
18 |
Correct |
32 ms |
34924 KB |
Output is correct |
19 |
Correct |
33 ms |
35692 KB |
Output is correct |
20 |
Correct |
34 ms |
35692 KB |
Output is correct |
21 |
Correct |
23 ms |
31212 KB |
Output is correct |
22 |
Correct |
32 ms |
35040 KB |
Output is correct |
23 |
Correct |
33 ms |
34540 KB |
Output is correct |
24 |
Correct |
36 ms |
35436 KB |
Output is correct |
25 |
Correct |
35 ms |
35692 KB |
Output is correct |
26 |
Correct |
40 ms |
35692 KB |
Output is correct |
27 |
Correct |
34 ms |
35692 KB |
Output is correct |
28 |
Correct |
36 ms |
35820 KB |
Output is correct |
29 |
Correct |
48 ms |
35732 KB |
Output is correct |
30 |
Correct |
40 ms |
35564 KB |
Output is correct |
31 |
Correct |
39 ms |
35820 KB |
Output is correct |
32 |
Correct |
35 ms |
35948 KB |
Output is correct |
33 |
Correct |
53 ms |
36588 KB |
Output is correct |
34 |
Correct |
52 ms |
36460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
30188 KB |
Output is correct |
2 |
Correct |
23 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30188 KB |
Output is correct |
4 |
Correct |
20 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30188 KB |
Output is correct |
7 |
Correct |
21 ms |
30316 KB |
Output is correct |
8 |
Correct |
22 ms |
30316 KB |
Output is correct |
9 |
Correct |
21 ms |
30316 KB |
Output is correct |
10 |
Correct |
25 ms |
30444 KB |
Output is correct |
11 |
Correct |
22 ms |
30444 KB |
Output is correct |
12 |
Correct |
21 ms |
30444 KB |
Output is correct |
13 |
Correct |
21 ms |
30444 KB |
Output is correct |
14 |
Correct |
22 ms |
30444 KB |
Output is correct |
15 |
Correct |
22 ms |
30444 KB |
Output is correct |
16 |
Correct |
22 ms |
30700 KB |
Output is correct |
17 |
Correct |
27 ms |
32364 KB |
Output is correct |
18 |
Correct |
32 ms |
34924 KB |
Output is correct |
19 |
Correct |
35 ms |
35692 KB |
Output is correct |
20 |
Correct |
34 ms |
35692 KB |
Output is correct |
21 |
Correct |
26 ms |
31212 KB |
Output is correct |
22 |
Correct |
36 ms |
35052 KB |
Output is correct |
23 |
Correct |
37 ms |
34540 KB |
Output is correct |
24 |
Correct |
36 ms |
35436 KB |
Output is correct |
25 |
Correct |
35 ms |
35692 KB |
Output is correct |
26 |
Correct |
35 ms |
35692 KB |
Output is correct |
27 |
Correct |
34 ms |
35692 KB |
Output is correct |
28 |
Correct |
37 ms |
35820 KB |
Output is correct |
29 |
Correct |
48 ms |
35564 KB |
Output is correct |
30 |
Correct |
37 ms |
35564 KB |
Output is correct |
31 |
Correct |
44 ms |
35820 KB |
Output is correct |
32 |
Correct |
35 ms |
35948 KB |
Output is correct |
33 |
Correct |
54 ms |
36460 KB |
Output is correct |
34 |
Correct |
54 ms |
36460 KB |
Output is correct |
35 |
Correct |
44 ms |
35180 KB |
Output is correct |
36 |
Correct |
30 ms |
33260 KB |
Output is correct |
37 |
Correct |
56 ms |
37740 KB |
Output is correct |
38 |
Correct |
58 ms |
37612 KB |
Output is correct |
39 |
Correct |
58 ms |
37612 KB |
Output is correct |
40 |
Correct |
56 ms |
37612 KB |
Output is correct |
41 |
Correct |
57 ms |
37612 KB |
Output is correct |
42 |
Correct |
39 ms |
36072 KB |
Output is correct |
43 |
Correct |
39 ms |
36072 KB |
Output is correct |
44 |
Correct |
40 ms |
36332 KB |
Output is correct |
45 |
Correct |
63 ms |
41708 KB |
Output is correct |
46 |
Correct |
64 ms |
41580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
30188 KB |
Output is correct |
2 |
Correct |
21 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30208 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30188 KB |
Output is correct |
7 |
Correct |
21 ms |
30188 KB |
Output is correct |
8 |
Correct |
21 ms |
30188 KB |
Output is correct |
9 |
Correct |
21 ms |
30316 KB |
Output is correct |
10 |
Correct |
21 ms |
30444 KB |
Output is correct |
11 |
Correct |
23 ms |
30444 KB |
Output is correct |
12 |
Correct |
22 ms |
30444 KB |
Output is correct |
13 |
Correct |
25 ms |
30444 KB |
Output is correct |
14 |
Correct |
23 ms |
30452 KB |
Output is correct |
15 |
Correct |
22 ms |
30444 KB |
Output is correct |
16 |
Correct |
22 ms |
30700 KB |
Output is correct |
17 |
Correct |
28 ms |
32364 KB |
Output is correct |
18 |
Correct |
32 ms |
35052 KB |
Output is correct |
19 |
Correct |
33 ms |
35692 KB |
Output is correct |
20 |
Correct |
34 ms |
35692 KB |
Output is correct |
21 |
Correct |
24 ms |
31212 KB |
Output is correct |
22 |
Correct |
32 ms |
35052 KB |
Output is correct |
23 |
Correct |
32 ms |
34540 KB |
Output is correct |
24 |
Correct |
38 ms |
35436 KB |
Output is correct |
25 |
Correct |
42 ms |
35640 KB |
Output is correct |
26 |
Correct |
35 ms |
35692 KB |
Output is correct |
27 |
Correct |
34 ms |
35692 KB |
Output is correct |
28 |
Correct |
36 ms |
35820 KB |
Output is correct |
29 |
Correct |
46 ms |
35564 KB |
Output is correct |
30 |
Correct |
37 ms |
35692 KB |
Output is correct |
31 |
Correct |
40 ms |
35820 KB |
Output is correct |
32 |
Correct |
36 ms |
35948 KB |
Output is correct |
33 |
Correct |
52 ms |
36552 KB |
Output is correct |
34 |
Correct |
52 ms |
36460 KB |
Output is correct |
35 |
Correct |
47 ms |
35180 KB |
Output is correct |
36 |
Correct |
30 ms |
33260 KB |
Output is correct |
37 |
Correct |
56 ms |
37740 KB |
Output is correct |
38 |
Correct |
60 ms |
37612 KB |
Output is correct |
39 |
Correct |
57 ms |
37760 KB |
Output is correct |
40 |
Correct |
57 ms |
37612 KB |
Output is correct |
41 |
Correct |
57 ms |
37612 KB |
Output is correct |
42 |
Correct |
39 ms |
36072 KB |
Output is correct |
43 |
Correct |
48 ms |
36072 KB |
Output is correct |
44 |
Correct |
40 ms |
36332 KB |
Output is correct |
45 |
Correct |
64 ms |
41708 KB |
Output is correct |
46 |
Correct |
64 ms |
41580 KB |
Output is correct |
47 |
Correct |
232 ms |
73648 KB |
Output is correct |
48 |
Correct |
188 ms |
96432 KB |
Output is correct |
49 |
Correct |
203 ms |
101868 KB |
Output is correct |
50 |
Correct |
211 ms |
108268 KB |
Output is correct |
51 |
Correct |
301 ms |
116652 KB |
Output is correct |
52 |
Correct |
310 ms |
116784 KB |
Output is correct |
53 |
Correct |
237 ms |
112364 KB |
Output is correct |
54 |
Correct |
214 ms |
111980 KB |
Output is correct |
55 |
Correct |
218 ms |
111980 KB |
Output is correct |
56 |
Correct |
235 ms |
114412 KB |
Output is correct |
57 |
Correct |
420 ms |
118772 KB |
Output is correct |
58 |
Correct |
232 ms |
112548 KB |
Output is correct |
59 |
Correct |
239 ms |
112744 KB |
Output is correct |
60 |
Correct |
244 ms |
113644 KB |
Output is correct |
61 |
Correct |
239 ms |
112872 KB |
Output is correct |
62 |
Correct |
290 ms |
117996 KB |
Output is correct |
63 |
Correct |
312 ms |
147876 KB |
Output is correct |
64 |
Correct |
328 ms |
161444 KB |
Output is correct |
65 |
Correct |
375 ms |
171972 KB |
Output is correct |
66 |
Correct |
704 ms |
195660 KB |
Output is correct |
67 |
Correct |
765 ms |
196264 KB |
Output is correct |