#include <bits/stdc++.h>
using namespace std;
#define N 30001
#define ll long long int
#define sz(x) (int)x.size()
#define ff first
#define ss second
int T, n, m, b[N], p[N], d[N];
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
d[i] = 1e5;
}
d[0] = 0;
int x = 0;
vector <int> v;
for(int i = 1; i < m; i++) v.push_back(i);
while(sz(v) > 0){
int mn = 1e5, ind = x;
for(auto i : v){
int k = (abs(b[i]-b[x]));
if(d[i] < mn) ind = i;
mn = min(d[i],mn);
if(k % p[x] != 0) continue;
d[i] = min(d[x] + (k/p[x]), d[i]);
if(d[i] < mn) ind = i;
mn = min(d[i],mn);
}
if(x == ind) break;
x = ind;
if(x == 1) break;
for(int i = 0; i < sz(v); i++){
if(v[i] == ind){
v.erase(v.begin() + i);
break;
}
}
}
if(d[1] == 1e5) d[1] = -1;
cout << d[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
348 KB |
Output is correct |
14 |
Correct |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
7 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
408 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
348 KB |
Output is correct |
14 |
Correct |
5 ms |
508 KB |
Output is correct |
15 |
Correct |
6 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
504 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
516 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
6 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
348 KB |
Output is correct |
34 |
Correct |
5 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
8 ms |
348 KB |
Output is correct |
14 |
Correct |
5 ms |
508 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
516 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
5 ms |
348 KB |
Output is correct |
34 |
Correct |
5 ms |
344 KB |
Output is correct |
35 |
Correct |
684 ms |
980 KB |
Output is correct |
36 |
Correct |
8 ms |
348 KB |
Output is correct |
37 |
Correct |
97 ms |
860 KB |
Output is correct |
38 |
Correct |
388 ms |
860 KB |
Output is correct |
39 |
Correct |
4 ms |
860 KB |
Output is correct |
40 |
Correct |
31 ms |
920 KB |
Output is correct |
41 |
Correct |
205 ms |
1032 KB |
Output is correct |
42 |
Correct |
4 ms |
860 KB |
Output is correct |
43 |
Correct |
11 ms |
1064 KB |
Output is correct |
44 |
Correct |
960 ms |
1036 KB |
Output is correct |
45 |
Correct |
841 ms |
1032 KB |
Output is correct |
46 |
Correct |
853 ms |
1036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
516 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
4 ms |
344 KB |
Output is correct |
34 |
Correct |
4 ms |
348 KB |
Output is correct |
35 |
Correct |
600 ms |
860 KB |
Output is correct |
36 |
Correct |
6 ms |
348 KB |
Output is correct |
37 |
Correct |
87 ms |
860 KB |
Output is correct |
38 |
Correct |
325 ms |
1112 KB |
Output is correct |
39 |
Correct |
4 ms |
860 KB |
Output is correct |
40 |
Correct |
20 ms |
1060 KB |
Output is correct |
41 |
Correct |
199 ms |
860 KB |
Output is correct |
42 |
Correct |
3 ms |
856 KB |
Output is correct |
43 |
Correct |
9 ms |
860 KB |
Output is correct |
44 |
Correct |
844 ms |
860 KB |
Output is correct |
45 |
Correct |
863 ms |
856 KB |
Output is correct |
46 |
Correct |
823 ms |
1040 KB |
Output is correct |
47 |
Correct |
148 ms |
856 KB |
Output is correct |
48 |
Correct |
3 ms |
860 KB |
Output is correct |
49 |
Correct |
3 ms |
856 KB |
Output is correct |
50 |
Correct |
2 ms |
604 KB |
Output is correct |
51 |
Correct |
726 ms |
860 KB |
Output is correct |
52 |
Correct |
818 ms |
1036 KB |
Output is correct |
53 |
Correct |
202 ms |
1032 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
995 ms |
860 KB |
Output is correct |
57 |
Correct |
1 ms |
344 KB |
Output is correct |
58 |
Correct |
801 ms |
1116 KB |
Output is correct |
59 |
Correct |
5 ms |
1112 KB |
Output is correct |
60 |
Correct |
5 ms |
1116 KB |
Output is correct |
61 |
Correct |
12 ms |
1316 KB |
Output is correct |
62 |
Incorrect |
942 ms |
1372 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |