#pragma GCC optimize("O3")
#pragma GCC target("sse2,sse3,ssse3,sse4,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= (b); --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)
#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second
const int BIG = 2*(int)(1e9);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
void solve();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
const int borneSuj = 30*1000 + 5;
const int borneTab = 200*borneSuj;
int gt[borneSuj][200];
int inv[borneTab];
bool witPrev[borneTab];
bool witSuiv[borneTab];
int arr = -1, dep=-1;
int nbPlaces, nbDoges, nbNoeuds;
int limSqd;
vector<int> doge[borneSuj];
void solve()
{
cin >> nbPlaces >> nbDoges;
chmax(nbPlaces, 20);
nbNoeuds = nbPlaces;
limSqd = sqrt(nbPlaces);
form2(distance, 1, limSqd) {
form(ini, distance) {
for (int place = ini; place < nbPlaces; place += distance) {
int corNod = nbNoeuds; ++nbNoeuds;
inv[corNod] = place+1;
gt[place][distance] = corNod;
if (place != ini) {
witPrev[corNod] = true;
witSuiv[corNod-1] = true;
}
}
}
}
form(iDoge, nbDoges) {
int pos, saut;
cin >> pos >> saut;
if (iDoge == 0) dep = pos;
if (iDoge == 1) arr = pos;
doge[pos].push_back(saut);
}
vector<int> md(nbNoeuds, BIG);
vector<int> etat(nbNoeuds, 2);
deque<int> m1;
m1.push_front(dep);
md[dep] = 0;
while (! m1.empty()) {
int nod = m1.front(); m1.pop_front();
etat[nod] = 0;
int dis = md[nod];
auto propag = [&] (int vo, int poids) {
if (etat[vo] == 2) {
md[vo] = dis + poids;
if (m1.empty() || md[m1.front()] < md[vo]) m1.push_back(vo);
else m1.push_front(vo);
etat[vo] = 1;
} else if (etat[vo] == 1) {
chmin(md[vo], dis + poids);
} else {
if (dis+poids < md[vo]) {
md[vo] = dis+poids;
etat[vo] = 1;
if (m1.empty() || md[m1.front()] < md[vo]) m1.push_back(vo);
else m1.push_front(vo);
}
}
};
if (nod < nbPlaces) {
int pos = nod;
for (int saut : doge[nod]) {
if (saut < limSqd) {
propag(gt[pos][saut], 0);
} else {
for (int dd = 1; pos + dd*saut < nbPlaces; ++dd) {
propag(pos+dd*saut, dd);
}
for (int dd = 1; pos - dd*saut >= 0; ++dd) {
propag(pos-dd*saut, dd);
}
}
}
}
if (nod >= nbPlaces) {
if (inv[nod]) propag(inv[nod]-1, 0);
if (witPrev[nod]) propag(nod-1, 1);
if (witSuiv[nod]) propag(nod+1, 1);
}
}
if (md[arr] == BIG) cout << "-1\n";
else cout << md[arr] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1152 KB |
Output is correct |
2 |
Correct |
3 ms |
1152 KB |
Output is correct |
3 |
Correct |
3 ms |
1048 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
3 ms |
1124 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1152 KB |
Output is correct |
2 |
Correct |
3 ms |
1152 KB |
Output is correct |
3 |
Correct |
4 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
4 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
3 ms |
1152 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
3 ms |
1152 KB |
Output is correct |
13 |
Correct |
4 ms |
1148 KB |
Output is correct |
14 |
Correct |
4 ms |
1152 KB |
Output is correct |
15 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
10 ms |
1024 KB |
Output is correct |
3 |
Correct |
4 ms |
1196 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
3 ms |
1152 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
4 ms |
1152 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
3 ms |
1152 KB |
Output is correct |
13 |
Correct |
3 ms |
1152 KB |
Output is correct |
14 |
Correct |
3 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1152 KB |
Output is correct |
16 |
Correct |
4 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
8 ms |
3456 KB |
Output is correct |
19 |
Correct |
6 ms |
3840 KB |
Output is correct |
20 |
Correct |
7 ms |
3968 KB |
Output is correct |
21 |
Correct |
4 ms |
1536 KB |
Output is correct |
22 |
Correct |
7 ms |
3584 KB |
Output is correct |
23 |
Correct |
7 ms |
3200 KB |
Output is correct |
24 |
Correct |
8 ms |
3712 KB |
Output is correct |
25 |
Correct |
7 ms |
3840 KB |
Output is correct |
26 |
Correct |
8 ms |
3840 KB |
Output is correct |
27 |
Correct |
7 ms |
3840 KB |
Output is correct |
28 |
Correct |
7 ms |
3968 KB |
Output is correct |
29 |
Correct |
7 ms |
3840 KB |
Output is correct |
30 |
Correct |
7 ms |
3840 KB |
Output is correct |
31 |
Correct |
6 ms |
3840 KB |
Output is correct |
32 |
Correct |
6 ms |
3840 KB |
Output is correct |
33 |
Correct |
8 ms |
3840 KB |
Output is correct |
34 |
Correct |
11 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1152 KB |
Output is correct |
2 |
Correct |
3 ms |
1152 KB |
Output is correct |
3 |
Correct |
4 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
3 ms |
1152 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
3 ms |
1152 KB |
Output is correct |
11 |
Correct |
4 ms |
1152 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
3 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1152 KB |
Output is correct |
16 |
Correct |
4 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
9 ms |
3456 KB |
Output is correct |
19 |
Correct |
6 ms |
3840 KB |
Output is correct |
20 |
Correct |
7 ms |
3840 KB |
Output is correct |
21 |
Correct |
3 ms |
1536 KB |
Output is correct |
22 |
Correct |
6 ms |
3456 KB |
Output is correct |
23 |
Correct |
5 ms |
3200 KB |
Output is correct |
24 |
Correct |
6 ms |
3712 KB |
Output is correct |
25 |
Correct |
6 ms |
3840 KB |
Output is correct |
26 |
Correct |
7 ms |
3840 KB |
Output is correct |
27 |
Correct |
8 ms |
3880 KB |
Output is correct |
28 |
Correct |
6 ms |
3968 KB |
Output is correct |
29 |
Correct |
7 ms |
3840 KB |
Output is correct |
30 |
Correct |
6 ms |
3840 KB |
Output is correct |
31 |
Correct |
6 ms |
3840 KB |
Output is correct |
32 |
Correct |
8 ms |
3840 KB |
Output is correct |
33 |
Correct |
11 ms |
3840 KB |
Output is correct |
34 |
Correct |
8 ms |
3840 KB |
Output is correct |
35 |
Correct |
17 ms |
3448 KB |
Output is correct |
36 |
Correct |
6 ms |
2432 KB |
Output is correct |
37 |
Correct |
16 ms |
3968 KB |
Output is correct |
38 |
Correct |
19 ms |
4224 KB |
Output is correct |
39 |
Correct |
18 ms |
4224 KB |
Output is correct |
40 |
Correct |
44 ms |
4276 KB |
Output is correct |
41 |
Correct |
22 ms |
4428 KB |
Output is correct |
42 |
Correct |
10 ms |
4224 KB |
Output is correct |
43 |
Correct |
15 ms |
4224 KB |
Output is correct |
44 |
Correct |
13 ms |
4224 KB |
Output is correct |
45 |
Correct |
18 ms |
4224 KB |
Output is correct |
46 |
Correct |
15 ms |
4224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1152 KB |
Output is correct |
2 |
Correct |
4 ms |
1152 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
3 ms |
1152 KB |
Output is correct |
6 |
Correct |
4 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
3 ms |
1152 KB |
Output is correct |
11 |
Correct |
4 ms |
1152 KB |
Output is correct |
12 |
Correct |
3 ms |
1152 KB |
Output is correct |
13 |
Correct |
3 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1152 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
6 ms |
3456 KB |
Output is correct |
19 |
Correct |
5 ms |
3840 KB |
Output is correct |
20 |
Correct |
6 ms |
3840 KB |
Output is correct |
21 |
Correct |
4 ms |
1536 KB |
Output is correct |
22 |
Correct |
7 ms |
3584 KB |
Output is correct |
23 |
Correct |
23 ms |
3200 KB |
Output is correct |
24 |
Correct |
7 ms |
3712 KB |
Output is correct |
25 |
Correct |
11 ms |
3968 KB |
Output is correct |
26 |
Correct |
8 ms |
3840 KB |
Output is correct |
27 |
Correct |
8 ms |
3840 KB |
Output is correct |
28 |
Correct |
12 ms |
3968 KB |
Output is correct |
29 |
Correct |
11 ms |
3840 KB |
Output is correct |
30 |
Correct |
6 ms |
3840 KB |
Output is correct |
31 |
Correct |
6 ms |
3840 KB |
Output is correct |
32 |
Correct |
6 ms |
3840 KB |
Output is correct |
33 |
Correct |
8 ms |
3840 KB |
Output is correct |
34 |
Correct |
11 ms |
3840 KB |
Output is correct |
35 |
Correct |
12 ms |
3328 KB |
Output is correct |
36 |
Correct |
7 ms |
2560 KB |
Output is correct |
37 |
Correct |
20 ms |
3968 KB |
Output is correct |
38 |
Correct |
21 ms |
4172 KB |
Output is correct |
39 |
Correct |
19 ms |
4224 KB |
Output is correct |
40 |
Correct |
19 ms |
4224 KB |
Output is correct |
41 |
Correct |
20 ms |
4224 KB |
Output is correct |
42 |
Correct |
11 ms |
4224 KB |
Output is correct |
43 |
Correct |
15 ms |
4224 KB |
Output is correct |
44 |
Correct |
11 ms |
4160 KB |
Output is correct |
45 |
Correct |
15 ms |
4224 KB |
Output is correct |
46 |
Correct |
21 ms |
4216 KB |
Output is correct |
47 |
Correct |
102 ms |
30020 KB |
Output is correct |
48 |
Correct |
165 ms |
68216 KB |
Output is correct |
49 |
Correct |
336 ms |
76732 KB |
Output is correct |
50 |
Correct |
386 ms |
87224 KB |
Output is correct |
51 |
Correct |
332 ms |
96356 KB |
Output is correct |
52 |
Correct |
303 ms |
96376 KB |
Output is correct |
53 |
Correct |
587 ms |
96432 KB |
Output is correct |
54 |
Correct |
605 ms |
95484 KB |
Output is correct |
55 |
Correct |
634 ms |
95480 KB |
Output is correct |
56 |
Correct |
756 ms |
96788 KB |
Output is correct |
57 |
Correct |
690 ms |
95536 KB |
Output is correct |
58 |
Correct |
586 ms |
95900 KB |
Output is correct |
59 |
Correct |
264 ms |
95836 KB |
Output is correct |
60 |
Correct |
256 ms |
95868 KB |
Output is correct |
61 |
Correct |
667 ms |
95992 KB |
Output is correct |
62 |
Correct |
606 ms |
97016 KB |
Output is correct |
63 |
Correct |
530 ms |
96016 KB |
Output is correct |
64 |
Correct |
583 ms |
96044 KB |
Output is correct |
65 |
Correct |
416 ms |
95932 KB |
Output is correct |
66 |
Correct |
541 ms |
96108 KB |
Output is correct |
67 |
Correct |
522 ms |
96172 KB |
Output is correct |