//Ye Rooz Khoob Miad ???
#include<bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int) (x).size
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
const int Inf = 1'061'109'567;
vector<int> In[N];
bool mark[N];
int n, m, B[N], P[N], D[N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
cin >> B[i] >> P[i];
In[B[i]].pb(P[i]);
}
map<pair<int, int>, bool> MP;
priority_queue<pair<int, int>> PQ;
memset(D, 63, sizeof D);
D[B[0]] = 0;
PQ.push({0, B[0]});
while(!PQ.empty()) {
int a = PQ.top().S;
PQ.pop();
if(mark[a]) continue;
mark[a] = true;
In[a].resize(distance(In[a].begin(), unique(all(In[a]))));
for(int x : In[a]) {
pair<int, int> w = {a % x, x};
if(MP[w]) continue;
else MP[w] = true;
for(int i = a; i < n; i += x) {
if(D[i] > D[a] + (i - a) / x) {
D[i] = D[a] + (i - a) / x;
PQ.push({-D[i], i});
}
}
for(int i = a; i >= 0; i -= x) {
if(D[i] > D[a] + (a - i) / x) {
D[i] = D[a] + (a - i) / x;
PQ.push({-D[i], i});
}
}
}
}
cout << (D[B[1]] == Inf ? -1 : D[B[1]]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
6 ms |
8532 KB |
Output is correct |
3 |
Correct |
6 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8472 KB |
Output is correct |
5 |
Correct |
6 ms |
8468 KB |
Output is correct |
6 |
Correct |
5 ms |
8556 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8480 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
4 ms |
8532 KB |
Output is correct |
9 |
Correct |
4 ms |
8532 KB |
Output is correct |
10 |
Correct |
4 ms |
8532 KB |
Output is correct |
11 |
Correct |
5 ms |
8660 KB |
Output is correct |
12 |
Correct |
5 ms |
8528 KB |
Output is correct |
13 |
Correct |
5 ms |
8532 KB |
Output is correct |
14 |
Correct |
5 ms |
8660 KB |
Output is correct |
15 |
Correct |
7 ms |
8660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8532 KB |
Output is correct |
6 |
Correct |
5 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
4 ms |
8532 KB |
Output is correct |
10 |
Correct |
4 ms |
8592 KB |
Output is correct |
11 |
Correct |
5 ms |
8660 KB |
Output is correct |
12 |
Correct |
6 ms |
8532 KB |
Output is correct |
13 |
Correct |
4 ms |
8512 KB |
Output is correct |
14 |
Correct |
6 ms |
8724 KB |
Output is correct |
15 |
Correct |
5 ms |
8644 KB |
Output is correct |
16 |
Correct |
4 ms |
8532 KB |
Output is correct |
17 |
Correct |
5 ms |
8676 KB |
Output is correct |
18 |
Correct |
4 ms |
8532 KB |
Output is correct |
19 |
Correct |
4 ms |
8528 KB |
Output is correct |
20 |
Correct |
5 ms |
8660 KB |
Output is correct |
21 |
Correct |
4 ms |
8532 KB |
Output is correct |
22 |
Correct |
4 ms |
8584 KB |
Output is correct |
23 |
Correct |
6 ms |
8660 KB |
Output is correct |
24 |
Correct |
6 ms |
8660 KB |
Output is correct |
25 |
Correct |
6 ms |
8660 KB |
Output is correct |
26 |
Correct |
5 ms |
8532 KB |
Output is correct |
27 |
Correct |
5 ms |
8532 KB |
Output is correct |
28 |
Correct |
6 ms |
8660 KB |
Output is correct |
29 |
Correct |
6 ms |
8660 KB |
Output is correct |
30 |
Correct |
7 ms |
8556 KB |
Output is correct |
31 |
Correct |
7 ms |
8628 KB |
Output is correct |
32 |
Correct |
5 ms |
8660 KB |
Output is correct |
33 |
Correct |
6 ms |
8660 KB |
Output is correct |
34 |
Correct |
7 ms |
8712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8556 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8484 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
5 ms |
8532 KB |
Output is correct |
11 |
Correct |
5 ms |
8660 KB |
Output is correct |
12 |
Correct |
9 ms |
8532 KB |
Output is correct |
13 |
Correct |
6 ms |
8532 KB |
Output is correct |
14 |
Correct |
6 ms |
8720 KB |
Output is correct |
15 |
Correct |
5 ms |
8660 KB |
Output is correct |
16 |
Correct |
5 ms |
8532 KB |
Output is correct |
17 |
Correct |
5 ms |
8660 KB |
Output is correct |
18 |
Correct |
5 ms |
8532 KB |
Output is correct |
19 |
Correct |
6 ms |
8532 KB |
Output is correct |
20 |
Correct |
5 ms |
8660 KB |
Output is correct |
21 |
Correct |
5 ms |
8532 KB |
Output is correct |
22 |
Correct |
5 ms |
8532 KB |
Output is correct |
23 |
Correct |
5 ms |
8660 KB |
Output is correct |
24 |
Correct |
7 ms |
8660 KB |
Output is correct |
25 |
Correct |
6 ms |
8660 KB |
Output is correct |
26 |
Correct |
5 ms |
8560 KB |
Output is correct |
27 |
Correct |
5 ms |
8532 KB |
Output is correct |
28 |
Correct |
6 ms |
8660 KB |
Output is correct |
29 |
Correct |
6 ms |
8576 KB |
Output is correct |
30 |
Correct |
5 ms |
8660 KB |
Output is correct |
31 |
Correct |
5 ms |
8660 KB |
Output is correct |
32 |
Correct |
6 ms |
8660 KB |
Output is correct |
33 |
Correct |
9 ms |
8660 KB |
Output is correct |
34 |
Correct |
7 ms |
8700 KB |
Output is correct |
35 |
Correct |
18 ms |
10552 KB |
Output is correct |
36 |
Correct |
7 ms |
8916 KB |
Output is correct |
37 |
Correct |
15 ms |
9940 KB |
Output is correct |
38 |
Correct |
21 ms |
10768 KB |
Output is correct |
39 |
Correct |
21 ms |
10788 KB |
Output is correct |
40 |
Correct |
22 ms |
10888 KB |
Output is correct |
41 |
Correct |
21 ms |
10820 KB |
Output is correct |
42 |
Correct |
8 ms |
8916 KB |
Output is correct |
43 |
Correct |
9 ms |
9044 KB |
Output is correct |
44 |
Correct |
8 ms |
8916 KB |
Output is correct |
45 |
Correct |
24 ms |
10812 KB |
Output is correct |
46 |
Correct |
19 ms |
10904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8552 KB |
Output is correct |
3 |
Correct |
4 ms |
8520 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
5 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
4 ms |
8532 KB |
Output is correct |
9 |
Correct |
4 ms |
8532 KB |
Output is correct |
10 |
Correct |
5 ms |
8552 KB |
Output is correct |
11 |
Correct |
5 ms |
8660 KB |
Output is correct |
12 |
Correct |
6 ms |
8532 KB |
Output is correct |
13 |
Correct |
5 ms |
8532 KB |
Output is correct |
14 |
Correct |
5 ms |
8660 KB |
Output is correct |
15 |
Correct |
5 ms |
8656 KB |
Output is correct |
16 |
Correct |
5 ms |
8532 KB |
Output is correct |
17 |
Correct |
5 ms |
8660 KB |
Output is correct |
18 |
Correct |
5 ms |
8532 KB |
Output is correct |
19 |
Correct |
4 ms |
8532 KB |
Output is correct |
20 |
Correct |
5 ms |
8660 KB |
Output is correct |
21 |
Correct |
4 ms |
8532 KB |
Output is correct |
22 |
Correct |
4 ms |
8532 KB |
Output is correct |
23 |
Correct |
5 ms |
8660 KB |
Output is correct |
24 |
Correct |
6 ms |
8660 KB |
Output is correct |
25 |
Correct |
6 ms |
8684 KB |
Output is correct |
26 |
Correct |
5 ms |
8608 KB |
Output is correct |
27 |
Correct |
5 ms |
8532 KB |
Output is correct |
28 |
Correct |
5 ms |
8660 KB |
Output is correct |
29 |
Correct |
6 ms |
8660 KB |
Output is correct |
30 |
Correct |
6 ms |
8660 KB |
Output is correct |
31 |
Correct |
5 ms |
8660 KB |
Output is correct |
32 |
Correct |
5 ms |
8660 KB |
Output is correct |
33 |
Correct |
6 ms |
8724 KB |
Output is correct |
34 |
Correct |
6 ms |
8784 KB |
Output is correct |
35 |
Correct |
19 ms |
10480 KB |
Output is correct |
36 |
Correct |
7 ms |
8916 KB |
Output is correct |
37 |
Correct |
16 ms |
9940 KB |
Output is correct |
38 |
Correct |
21 ms |
10860 KB |
Output is correct |
39 |
Correct |
21 ms |
10772 KB |
Output is correct |
40 |
Correct |
22 ms |
10812 KB |
Output is correct |
41 |
Correct |
21 ms |
10836 KB |
Output is correct |
42 |
Correct |
8 ms |
8916 KB |
Output is correct |
43 |
Correct |
8 ms |
8968 KB |
Output is correct |
44 |
Correct |
8 ms |
9032 KB |
Output is correct |
45 |
Correct |
19 ms |
10812 KB |
Output is correct |
46 |
Correct |
19 ms |
10836 KB |
Output is correct |
47 |
Correct |
29 ms |
10832 KB |
Output is correct |
48 |
Correct |
10 ms |
9152 KB |
Output is correct |
49 |
Correct |
9 ms |
9172 KB |
Output is correct |
50 |
Correct |
8 ms |
8916 KB |
Output is correct |
51 |
Correct |
39 ms |
11716 KB |
Output is correct |
52 |
Correct |
40 ms |
11716 KB |
Output is correct |
53 |
Correct |
33 ms |
11588 KB |
Output is correct |
54 |
Correct |
7 ms |
8788 KB |
Output is correct |
55 |
Correct |
6 ms |
8788 KB |
Output is correct |
56 |
Correct |
13 ms |
10072 KB |
Output is correct |
57 |
Correct |
31 ms |
9756 KB |
Output is correct |
58 |
Correct |
12 ms |
9200 KB |
Output is correct |
59 |
Correct |
11 ms |
9172 KB |
Output is correct |
60 |
Correct |
11 ms |
9228 KB |
Output is correct |
61 |
Correct |
12 ms |
9240 KB |
Output is correct |
62 |
Correct |
23 ms |
11600 KB |
Output is correct |
63 |
Correct |
43 ms |
11572 KB |
Output is correct |
64 |
Correct |
44 ms |
11548 KB |
Output is correct |
65 |
Correct |
52 ms |
11556 KB |
Output is correct |
66 |
Correct |
86 ms |
11864 KB |
Output is correct |
67 |
Correct |
83 ms |
11868 KB |
Output is correct |