#include <bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
using ll = long long;
const int MAX = 3e4;
const int SQRT = 175;
int nbSauts[MAX][SQRT+1];
struct Situation
{
int building, power, sauts;
bool operator<(Situation other) const
{
return sauts > other.sauts;
}
};
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbBat, nbDog;
cin >> nbBat >> nbDog;
vector<pair<int, int> > dogs(nbDog);
set<int> dogsInBat[MAX];
for (auto &[bat, pow] : dogs)
{
cin >> bat >> pow;
dogsInBat[bat].insert(pow);
}
for (int i(0); i < nbBat; ++i)
for (int j(0); j <= SQRT; ++j)
nbSauts[i][j] = 1e9;
nbSauts[dogs[0].first][SQRT] = 0;
priority_queue<Situation> q;
q.push({dogs[0].first, SQRT, 0});
while (SZ(q))
{
auto situ = q.top(); q.pop();
if (situ.building == dogs[1].first)
{
cout << situ.sauts << endl;
return 0;
}
if (situ.sauts > nbSauts[situ.building][situ.power])
continue ;
if (situ.power == SQRT)
{
for (auto p : dogsInBat[situ.building])
{
if (p < SQRT)
{
if (situ.building - p >= 0 and nbSauts[situ.building-p][p] > situ.sauts+1)
{
q.push({situ.building-p, p, situ.sauts+1});
nbSauts[situ.building-p][p] = situ.sauts + 1;
}
if (situ.building+p < nbBat and nbSauts[situ.building+p][p]>situ.sauts+1)
{
q.push({situ.building+p, p, situ.sauts+1});
nbSauts[situ.building+p][p] = situ.sauts + 1;
}
}
else
{
for (int i(1); p * i + situ.building < nbBat; ++i)
{
if (nbSauts[situ.building+i*p][SQRT] > situ.sauts+i)
{
q.push({situ.building+i*p, SQRT, situ.sauts + i});
nbSauts[situ.building+i*p][SQRT] = situ.sauts + i;
}
}
for (int i(-1); p*i + situ.building >= 0; --i)
{
if (nbSauts[situ.building+i*p][SQRT] > situ.sauts-i)
{
q.push({situ.building+i*p, SQRT, situ.sauts - i});
nbSauts[situ.building+i*p][SQRT] = situ.sauts -i;
}
}
}
}
}
else
{
int p = situ.power;
if (situ.building - p >= 0 and nbSauts[situ.building-p][p] > situ.sauts+1)
{
q.push({situ.building-p, p, situ.sauts+1});
nbSauts[situ.building-p][p] = situ.sauts + 1;
}
if (situ.building+p < nbBat and nbSauts[situ.building+p][p] > situ.sauts+1)
{
q.push({situ.building+p, p,situ.sauts+1});
nbSauts[situ.building+p][p] = situ.sauts + 1;
}
if (nbSauts[situ.building][SQRT] > situ.sauts)
{
nbSauts[situ.building][SQRT] = situ.sauts;
q.push({situ.building, SQRT, situ.sauts});
}
}
}
cout << -1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1664 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
1 ms |
1792 KB |
Output is correct |
11 |
Correct |
3 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
2 ms |
2048 KB |
Output is correct |
15 |
Correct |
3 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1792 KB |
Output is correct |
2 |
Correct |
2 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
2 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
3 ms |
2048 KB |
Output is correct |
15 |
Correct |
2 ms |
2048 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
2 ms |
2432 KB |
Output is correct |
18 |
Correct |
2 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3200 KB |
Output is correct |
20 |
Correct |
3 ms |
3200 KB |
Output is correct |
21 |
Correct |
2 ms |
2048 KB |
Output is correct |
22 |
Correct |
3 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2944 KB |
Output is correct |
24 |
Correct |
3 ms |
3200 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3200 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
3 ms |
3200 KB |
Output is correct |
31 |
Correct |
5 ms |
3200 KB |
Output is correct |
32 |
Correct |
4 ms |
3200 KB |
Output is correct |
33 |
Correct |
13 ms |
3328 KB |
Output is correct |
34 |
Correct |
9 ms |
3328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
1 ms |
1792 KB |
Output is correct |
11 |
Correct |
3 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1920 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
3 ms |
2048 KB |
Output is correct |
15 |
Correct |
2 ms |
2048 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
2 ms |
2432 KB |
Output is correct |
18 |
Correct |
2 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3200 KB |
Output is correct |
20 |
Correct |
3 ms |
3200 KB |
Output is correct |
21 |
Correct |
2 ms |
2048 KB |
Output is correct |
22 |
Correct |
3 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2944 KB |
Output is correct |
24 |
Correct |
3 ms |
3200 KB |
Output is correct |
25 |
Correct |
4 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3200 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
3 ms |
3072 KB |
Output is correct |
31 |
Correct |
5 ms |
3200 KB |
Output is correct |
32 |
Correct |
4 ms |
3200 KB |
Output is correct |
33 |
Correct |
13 ms |
3328 KB |
Output is correct |
34 |
Correct |
8 ms |
3328 KB |
Output is correct |
35 |
Correct |
12 ms |
4352 KB |
Output is correct |
36 |
Correct |
3 ms |
2816 KB |
Output is correct |
37 |
Correct |
10 ms |
4352 KB |
Output is correct |
38 |
Correct |
13 ms |
4864 KB |
Output is correct |
39 |
Correct |
13 ms |
4736 KB |
Output is correct |
40 |
Correct |
13 ms |
4736 KB |
Output is correct |
41 |
Correct |
15 ms |
5120 KB |
Output is correct |
42 |
Correct |
7 ms |
3328 KB |
Output is correct |
43 |
Correct |
7 ms |
3328 KB |
Output is correct |
44 |
Correct |
7 ms |
3456 KB |
Output is correct |
45 |
Correct |
49 ms |
5312 KB |
Output is correct |
46 |
Correct |
30 ms |
5312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
1 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
3 ms |
2048 KB |
Output is correct |
15 |
Correct |
2 ms |
2048 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
2 ms |
2432 KB |
Output is correct |
18 |
Correct |
2 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3200 KB |
Output is correct |
20 |
Correct |
3 ms |
3200 KB |
Output is correct |
21 |
Correct |
2 ms |
2048 KB |
Output is correct |
22 |
Correct |
2 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2944 KB |
Output is correct |
24 |
Correct |
3 ms |
3200 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
3 ms |
3200 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
4 ms |
3200 KB |
Output is correct |
31 |
Correct |
5 ms |
3200 KB |
Output is correct |
32 |
Correct |
5 ms |
3200 KB |
Output is correct |
33 |
Correct |
13 ms |
3328 KB |
Output is correct |
34 |
Correct |
9 ms |
3328 KB |
Output is correct |
35 |
Correct |
13 ms |
4352 KB |
Output is correct |
36 |
Correct |
3 ms |
2816 KB |
Output is correct |
37 |
Correct |
10 ms |
4352 KB |
Output is correct |
38 |
Correct |
15 ms |
4864 KB |
Output is correct |
39 |
Correct |
13 ms |
4768 KB |
Output is correct |
40 |
Correct |
16 ms |
4736 KB |
Output is correct |
41 |
Correct |
18 ms |
4992 KB |
Output is correct |
42 |
Correct |
7 ms |
3328 KB |
Output is correct |
43 |
Correct |
8 ms |
3328 KB |
Output is correct |
44 |
Correct |
7 ms |
3456 KB |
Output is correct |
45 |
Correct |
51 ms |
5312 KB |
Output is correct |
46 |
Correct |
31 ms |
5312 KB |
Output is correct |
47 |
Correct |
20 ms |
12096 KB |
Output is correct |
48 |
Correct |
21 ms |
18944 KB |
Output is correct |
49 |
Correct |
19 ms |
20352 KB |
Output is correct |
50 |
Correct |
19 ms |
21760 KB |
Output is correct |
51 |
Correct |
33 ms |
24504 KB |
Output is correct |
52 |
Correct |
34 ms |
24512 KB |
Output is correct |
53 |
Correct |
25 ms |
24056 KB |
Output is correct |
54 |
Correct |
15 ms |
22400 KB |
Output is correct |
55 |
Correct |
15 ms |
22400 KB |
Output is correct |
56 |
Correct |
24 ms |
24064 KB |
Output is correct |
57 |
Correct |
15 ms |
22528 KB |
Output is correct |
58 |
Correct |
23 ms |
22656 KB |
Output is correct |
59 |
Correct |
22 ms |
22656 KB |
Output is correct |
60 |
Correct |
22 ms |
22656 KB |
Output is correct |
61 |
Correct |
23 ms |
22784 KB |
Output is correct |
62 |
Correct |
49 ms |
24064 KB |
Output is correct |
63 |
Correct |
72 ms |
25712 KB |
Output is correct |
64 |
Correct |
82 ms |
25712 KB |
Output is correct |
65 |
Correct |
285 ms |
25720 KB |
Output is correct |
66 |
Correct |
972 ms |
25836 KB |
Output is correct |
67 |
Correct |
389 ms |
25984 KB |
Output is correct |