// 4
// ...
#ifndef LOCAL
#define NDEBUG 1
#endif
#include<bits/stdc++.h>
int main(){
std::ios::sync_with_stdio(0);std::cin.tie(0);
int numBlock, numDog; std::cin>>numBlock>>numDog;
std::vector<std::vector<int>> lengthsAt(numBlock);
struct T{
int home, power;
bool operator==(T other) const{return std::tie(home, power)==std::tie(other.home, other.power);}
bool operator<(T other) const{return std::tie(home, power)<std::tie(other.home, other.power);}
};
std::vector<T> data(numDog);
for(auto& [home, power]: data){
std::cin>>home>>power;
}
data.erase(std::remove_if(2+begin(data), end(data), [&](T it){return it==data[0] or it==data[1];}),
data.end());
std::sort(begin(data)+2, end(data));
data.erase(std::unique(begin(data)+2, end(data)), end(data));
numDog=(int)data.size();
for(auto [home, power]: data){
lengthsAt[home].push_back(power);
}
for(auto& it: lengthsAt){
std::sort(begin(it), end(it));
it.erase(std::unique(begin(it), end(it)), end(it));
}
struct EdgeCluster{int first, lastInclusive, power;};
std::vector<std::vector<EdgeCluster>> add(numBlock);
/*
auto const indexBlock=[&](int block){return block;};
auto const indexDog=[&](int dog){return dog+numBlock;};
*/
for(int dog=0; dog<numDog; ++dog){
auto [home, power]=data[dog];
for(int next=home-power; next>=0; next-=power){
if(std::binary_search(begin(lengthsAt[next]), end(lengthsAt[next]), power) or next-power<0){
add[home].push_back({next, home-power, power});
break;
}
}
for(int next=home+power; next<numBlock; next+=power){
if(std::binary_search(begin(lengthsAt[next]), end(lengthsAt[next]), power) or next+power>=numBlock){
add[home].push_back({home+power, next, power});
break;
}
}
}
struct State{int node, distance;
bool operator<(State other) const{return distance>other.distance;}
};
std::vector<int> distance(add.size(), INT_MAX);
std::priority_queue<State> queue;
queue.push({data[0].home, distance[data[0].home]=0});
while(not queue.empty()){
auto [node, curDistance]=queue.top(); queue.pop();
if(curDistance!=distance[node]) continue;
if(node==data[1].home){
std::cout<<curDistance<<'\n';
return 0;
}
for(auto [first, lastInclusive, power]: add[node]){
if(first==node+power){
for(int next=first, length=1; next<=lastInclusive; next+=power,++length){
auto nextDistance=curDistance+length;
if(nextDistance<distance[next]){
queue.push({next, distance[next]=nextDistance});
}
}
}else{
assert(lastInclusive==node-power);
for(int next=lastInclusive, length=1; next>=first; next-=power,++length){
auto nextDistance=curDistance+length;
if(nextDistance<distance[next]){
queue.push({next, distance[next]=nextDistance});
}
}
}
}
}
std::cout<<"-1\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
416 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
896 KB |
Output is correct |
34 |
Correct |
3 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
896 KB |
Output is correct |
34 |
Correct |
3 ms |
896 KB |
Output is correct |
35 |
Correct |
12 ms |
1152 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
12 ms |
1280 KB |
Output is correct |
38 |
Correct |
15 ms |
1536 KB |
Output is correct |
39 |
Correct |
15 ms |
1408 KB |
Output is correct |
40 |
Correct |
17 ms |
1536 KB |
Output is correct |
41 |
Correct |
15 ms |
1408 KB |
Output is correct |
42 |
Correct |
8 ms |
640 KB |
Output is correct |
43 |
Correct |
7 ms |
768 KB |
Output is correct |
44 |
Correct |
7 ms |
768 KB |
Output is correct |
45 |
Correct |
14 ms |
1792 KB |
Output is correct |
46 |
Correct |
13 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
896 KB |
Output is correct |
34 |
Correct |
4 ms |
896 KB |
Output is correct |
35 |
Correct |
16 ms |
1152 KB |
Output is correct |
36 |
Correct |
3 ms |
640 KB |
Output is correct |
37 |
Correct |
12 ms |
1280 KB |
Output is correct |
38 |
Correct |
15 ms |
1536 KB |
Output is correct |
39 |
Correct |
15 ms |
1408 KB |
Output is correct |
40 |
Correct |
15 ms |
1536 KB |
Output is correct |
41 |
Correct |
16 ms |
1408 KB |
Output is correct |
42 |
Correct |
7 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
8 ms |
768 KB |
Output is correct |
45 |
Correct |
14 ms |
1664 KB |
Output is correct |
46 |
Correct |
13 ms |
1664 KB |
Output is correct |
47 |
Correct |
26 ms |
2932 KB |
Output is correct |
48 |
Correct |
14 ms |
2560 KB |
Output is correct |
49 |
Correct |
13 ms |
2688 KB |
Output is correct |
50 |
Correct |
10 ms |
2560 KB |
Output is correct |
51 |
Correct |
26 ms |
3964 KB |
Output is correct |
52 |
Correct |
26 ms |
3964 KB |
Output is correct |
53 |
Correct |
16 ms |
3072 KB |
Output is correct |
54 |
Correct |
3 ms |
2048 KB |
Output is correct |
55 |
Correct |
3 ms |
2176 KB |
Output is correct |
56 |
Correct |
16 ms |
3968 KB |
Output is correct |
57 |
Correct |
6 ms |
2432 KB |
Output is correct |
58 |
Correct |
10 ms |
2304 KB |
Output is correct |
59 |
Correct |
11 ms |
2560 KB |
Output is correct |
60 |
Correct |
12 ms |
2688 KB |
Output is correct |
61 |
Correct |
11 ms |
2740 KB |
Output is correct |
62 |
Correct |
18 ms |
3840 KB |
Output is correct |
63 |
Correct |
39 ms |
4216 KB |
Output is correct |
64 |
Correct |
41 ms |
4208 KB |
Output is correct |
65 |
Correct |
50 ms |
5256 KB |
Output is correct |
66 |
Correct |
114 ms |
7272 KB |
Output is correct |
67 |
Correct |
71 ms |
7144 KB |
Output is correct |