#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <unordered_map>
int x, dog_cnt;
std::vector<std::pair<int,int>> dogs;
std::unordered_map<int,int> dist[30005];
std::vector<int> dogs_in[30005];
int astar(int pos, int dog) {
std::priority_queue<std::tuple<int,int,int>, std::vector<std::tuple<int,int,int>>, std::greater<std::tuple<int,int,int>>> pq;
pq.emplace(-0x3f3f3f3f+std::abs(pos-dogs[1].first),pos,dog);
dist[pos][dog] = -0x3f3f3f3f;
int iterations = 3000000;
while (!pq.empty()&&iterations--) {
auto [c, pos, dog] = pq.top();
pq.pop();
if (pos-dogs[dog].second>=0) {
if (!dist[pos-dogs[dog].second].count(dog)||dist[pos-dogs[dog].second][dog]>dist[pos][dog]+1) {
dist[pos-dogs[dog].second][dog] = dist[pos][dog]+1;
pq.emplace(dist[pos-dogs[dog].second][dog]+std::abs((pos-dogs[dog].second)-dogs[1].first),
pos-dogs[dog].second,
dog);
}
}
if (pos+dogs[dog].second<x) {
if (!dist[pos+dogs[dog].second].count(dog)||dist[pos+dogs[dog].second][dog]>dist[pos][dog]+1) {
dist[pos+dogs[dog].second][dog] = dist[pos][dog]+1;
pq.emplace(dist[pos+dogs[dog].second][dog]+std::abs((pos+dogs[dog].second)-dogs[1].first),
pos+dogs[dog].second,
dog);
}
}
if (!dogs_in[pos].empty()) {
int next_idx = std::lower_bound(dogs_in[pos].begin(),dogs_in[pos].end(),dog)-dogs_in[pos].begin();
if (next_idx+1>=dogs_in[pos].size()||dogs_in[pos][next_idx]!=dog) {
next_idx = -1;
}
next_idx = dogs_in[pos][next_idx+1];
if (!dist[pos].count(next_idx)||dist[pos][next_idx]>dist[pos][dog]) {
dist[pos][next_idx] = dist[pos][dog];
pq.emplace(dist[pos][next_idx]+std::abs(pos-dogs[1].first),
pos,
next_idx);
}
next_idx = dogs_in[pos][0];
if (!dist[pos].count(next_idx)||dist[pos][next_idx]>dist[pos][dog]) {
dist[pos][next_idx] = dist[pos][dog];
pq.emplace(dist[pos][next_idx]+std::abs(pos-dogs[1].first),
pos,
next_idx);
}
}
}
int min = 0x3f3f3f3f;
for (int i = 0; i < dog_cnt; i++) {
if (!dist[dogs[1].first].count(i)) {
continue;
}
min = std::min(min, dist[dogs[1].first][i]+0x3f3f3f3f);
}
return ((min==0x3f3f3f3f) ? -1 : min);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> x >> dog_cnt;
dogs.reserve(dog_cnt);
for (int i = 0, a, b; i < dog_cnt; i++) {
std::cin >> a >> b;
dogs.emplace_back(a,b);
dogs_in[a].emplace_back(i);
}
for (int i = 0; i < 30005; i++) {
std::sort(dogs_in[i].begin(),dogs_in[i].end());
}
std::cout << astar(dogs[0].first, 0) << "\n";
}
Compilation message
skyscraper.cpp: In function 'int astar(int, int)':
skyscraper.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (next_idx+1>=dogs_in[pos].size()||dogs_in[pos][next_idx]!=dog) {
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2812 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
6 ms |
3164 KB |
Output is correct |
12 |
Correct |
29 ms |
10868 KB |
Output is correct |
13 |
Correct |
34 ms |
10832 KB |
Output is correct |
14 |
Correct |
4 ms |
3164 KB |
Output is correct |
15 |
Correct |
4 ms |
2932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2864 KB |
Output is correct |
11 |
Correct |
5 ms |
3292 KB |
Output is correct |
12 |
Correct |
27 ms |
10844 KB |
Output is correct |
13 |
Correct |
34 ms |
10860 KB |
Output is correct |
14 |
Correct |
4 ms |
2908 KB |
Output is correct |
15 |
Correct |
4 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
20 ms |
3744 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2812 KB |
Output is correct |
20 |
Correct |
851 ms |
128664 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
5 ms |
3144 KB |
Output is correct |
24 |
Correct |
16 ms |
3624 KB |
Output is correct |
25 |
Correct |
9 ms |
3420 KB |
Output is correct |
26 |
Correct |
438 ms |
127468 KB |
Output is correct |
27 |
Correct |
431 ms |
129568 KB |
Output is correct |
28 |
Correct |
7 ms |
3672 KB |
Output is correct |
29 |
Correct |
15 ms |
5724 KB |
Output is correct |
30 |
Correct |
4 ms |
3420 KB |
Output is correct |
31 |
Correct |
7 ms |
4188 KB |
Output is correct |
32 |
Correct |
5 ms |
3676 KB |
Output is correct |
33 |
Correct |
52 ms |
8532 KB |
Output is correct |
34 |
Correct |
107 ms |
8536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2808 KB |
Output is correct |
9 |
Correct |
1 ms |
2804 KB |
Output is correct |
10 |
Correct |
3 ms |
2908 KB |
Output is correct |
11 |
Correct |
5 ms |
3164 KB |
Output is correct |
12 |
Correct |
31 ms |
10756 KB |
Output is correct |
13 |
Correct |
35 ms |
10820 KB |
Output is correct |
14 |
Correct |
5 ms |
2904 KB |
Output is correct |
15 |
Correct |
3 ms |
2904 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
21 ms |
3672 KB |
Output is correct |
18 |
Correct |
2 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
772 ms |
128592 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
6 ms |
3164 KB |
Output is correct |
24 |
Correct |
15 ms |
3512 KB |
Output is correct |
25 |
Correct |
10 ms |
3420 KB |
Output is correct |
26 |
Correct |
455 ms |
127432 KB |
Output is correct |
27 |
Correct |
448 ms |
129364 KB |
Output is correct |
28 |
Correct |
6 ms |
3672 KB |
Output is correct |
29 |
Correct |
13 ms |
5560 KB |
Output is correct |
30 |
Correct |
4 ms |
3420 KB |
Output is correct |
31 |
Correct |
7 ms |
4188 KB |
Output is correct |
32 |
Correct |
6 ms |
3676 KB |
Output is correct |
33 |
Correct |
49 ms |
8364 KB |
Output is correct |
34 |
Correct |
87 ms |
8424 KB |
Output is correct |
35 |
Correct |
152 ms |
10000 KB |
Output is correct |
36 |
Correct |
23 ms |
3652 KB |
Output is correct |
37 |
Correct |
273 ms |
16812 KB |
Output is correct |
38 |
Correct |
296 ms |
14288 KB |
Output is correct |
39 |
Correct |
178 ms |
15248 KB |
Output is correct |
40 |
Incorrect |
972 ms |
16884 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
5 ms |
3164 KB |
Output is correct |
12 |
Correct |
27 ms |
10784 KB |
Output is correct |
13 |
Correct |
35 ms |
10832 KB |
Output is correct |
14 |
Correct |
4 ms |
2908 KB |
Output is correct |
15 |
Correct |
3 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
21 ms |
3800 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
861 ms |
128672 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
6 ms |
3164 KB |
Output is correct |
24 |
Correct |
20 ms |
3516 KB |
Output is correct |
25 |
Correct |
10 ms |
3420 KB |
Output is correct |
26 |
Correct |
456 ms |
127508 KB |
Output is correct |
27 |
Correct |
442 ms |
129364 KB |
Output is correct |
28 |
Correct |
7 ms |
3672 KB |
Output is correct |
29 |
Correct |
18 ms |
5724 KB |
Output is correct |
30 |
Correct |
4 ms |
3416 KB |
Output is correct |
31 |
Correct |
8 ms |
4244 KB |
Output is correct |
32 |
Correct |
5 ms |
3676 KB |
Output is correct |
33 |
Correct |
51 ms |
8568 KB |
Output is correct |
34 |
Correct |
92 ms |
8532 KB |
Output is correct |
35 |
Correct |
147 ms |
9868 KB |
Output is correct |
36 |
Correct |
24 ms |
3676 KB |
Output is correct |
37 |
Correct |
265 ms |
16432 KB |
Output is correct |
38 |
Correct |
292 ms |
13832 KB |
Output is correct |
39 |
Correct |
183 ms |
15208 KB |
Output is correct |
40 |
Incorrect |
982 ms |
16696 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |