#include<bits/stdc++.h>
using namespace std;
const int MAXN = 30010;
const int BLOCK = 1000;
vector<int> p2bs[MAXN];
vector<int> col2p[MAXN];
vector<pair<int,int>> col2col[MAXN];
int ans[BLOCK][MAXN];
int& ans_at(int p, int x){
return ans[p + 1][x];
}
int32_t main(){
cin.tie(NULL)->sync_with_stdio(false);
int n, m; cin >> n >> m;
for(int i = 0; i < BLOCK; ++i)
for(int j = 0; j < n; ++j)
ans[i][j] = INT_MAX;
int B[2], P[2];
cin >> B[0] >> P[0];
cin >> B[1] >> P[1];
for(int i = 2; i < m; ++i){
int b, p; cin >> b >> p;
p2bs[p].push_back(b);
}
for(int p = 1; p < MAXN; ++p){
sort(p2bs[p].begin(), p2bs[p].end());
p2bs[p].erase(unique(p2bs[p].begin(), p2bs[p].end()), p2bs[p].end());
if(p + 1 < BLOCK){
for(auto b : p2bs[p]){
col2p[b].push_back(p);
}
}else{
for(auto b : p2bs[p]){
for(int i = b % p; i < n; i += p){
int dist = abs(i - b) / p;
col2col[b].push_back({i, dist});
}
}
}
}
struct Node{
int i, x;
// se i == -1, eh col
int cost;
bool operator<(const Node &n) const{
return cost > n.cost;
}
};
priority_queue<Node> pq;
deque<Node> q;
for(int i = (B[0] % P[0]); i < n; i += P[0]){
int dist = abs(i - B[0]) / P[0];
pq.push({-1, i, ans_at(-1, i) = dist});
}
// print();
while(pq.size() + q.size() > 0){
auto maybe_update = [&](int p, int x, int val, int d){
if(ans_at(p, x) > val){
Node X = {p, x, ans_at(p, x) = val};
if(d == 0) q.push_front(X);
else if(d == 1) q.push_back(X);
else pq.push(X);
}
};
Node nxt;
{
if(q.size()){
nxt = q.front();
q.pop_front();
}else{
nxt = pq.top(); pq.pop();
}
}
auto [i, x, cost] = nxt;
if(cost > ans_at(i, x)) continue;
// cout << i << " " << x << " " << cost << "\n";
if(i == -1){
for(auto p : col2p[x]){
maybe_update(p, x, cost, 0);
}
for(auto [y, ec] : col2col[x]){
maybe_update(-1, y, cost + ec, 2);
}
}else if(i + 1 < BLOCK){
// sou power i na coluna x
maybe_update(-1, x, cost, 0);
if(x + i < n){
maybe_update(i, x + i, cost + 1, 1);
}
if(x - i >= 0){
maybe_update(i, x - i, cost + 1, 1);
}
}
// print();
}
cout << ((ans_at(-1, B[1]) == INT_MAX)? -1 : ans_at(-1, B[1])) << "\n";
}
Compilation message
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:94:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
94 | auto [i, x, cost] = nxt;
| ^
skyscraper.cpp:103:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
103 | for(auto [y, ec] : col2col[x]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6660 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6660 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
5 ms |
6660 KB |
Output is correct |
7 |
Correct |
4 ms |
6664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
4 ms |
6612 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6664 KB |
Output is correct |
6 |
Correct |
4 ms |
6592 KB |
Output is correct |
7 |
Correct |
3 ms |
6612 KB |
Output is correct |
8 |
Correct |
4 ms |
6788 KB |
Output is correct |
9 |
Correct |
4 ms |
6868 KB |
Output is correct |
10 |
Correct |
4 ms |
7048 KB |
Output is correct |
11 |
Correct |
5 ms |
6996 KB |
Output is correct |
12 |
Correct |
4 ms |
6996 KB |
Output is correct |
13 |
Correct |
4 ms |
6996 KB |
Output is correct |
14 |
Correct |
5 ms |
7056 KB |
Output is correct |
15 |
Correct |
6 ms |
7048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
4 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
4 ms |
6660 KB |
Output is correct |
5 |
Correct |
5 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
4 ms |
6664 KB |
Output is correct |
8 |
Correct |
3 ms |
6796 KB |
Output is correct |
9 |
Correct |
3 ms |
6868 KB |
Output is correct |
10 |
Correct |
5 ms |
6996 KB |
Output is correct |
11 |
Correct |
5 ms |
6996 KB |
Output is correct |
12 |
Correct |
5 ms |
6996 KB |
Output is correct |
13 |
Correct |
4 ms |
6996 KB |
Output is correct |
14 |
Correct |
6 ms |
6996 KB |
Output is correct |
15 |
Correct |
4 ms |
6996 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
9616 KB |
Output is correct |
18 |
Correct |
7 ms |
13524 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14548 KB |
Output is correct |
21 |
Correct |
5 ms |
8148 KB |
Output is correct |
22 |
Correct |
7 ms |
13652 KB |
Output is correct |
23 |
Correct |
7 ms |
12884 KB |
Output is correct |
24 |
Correct |
9 ms |
14164 KB |
Output is correct |
25 |
Correct |
8 ms |
14548 KB |
Output is correct |
26 |
Correct |
9 ms |
14492 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14420 KB |
Output is correct |
30 |
Correct |
8 ms |
14420 KB |
Output is correct |
31 |
Correct |
11 ms |
14420 KB |
Output is correct |
32 |
Correct |
8 ms |
14504 KB |
Output is correct |
33 |
Correct |
10 ms |
14496 KB |
Output is correct |
34 |
Correct |
10 ms |
14480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
4 ms |
6660 KB |
Output is correct |
3 |
Correct |
4 ms |
6612 KB |
Output is correct |
4 |
Correct |
5 ms |
6612 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6684 KB |
Output is correct |
7 |
Correct |
4 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6868 KB |
Output is correct |
10 |
Correct |
4 ms |
6996 KB |
Output is correct |
11 |
Correct |
4 ms |
6996 KB |
Output is correct |
12 |
Correct |
3 ms |
6996 KB |
Output is correct |
13 |
Correct |
4 ms |
6996 KB |
Output is correct |
14 |
Correct |
4 ms |
6996 KB |
Output is correct |
15 |
Correct |
5 ms |
6996 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
8 ms |
9624 KB |
Output is correct |
18 |
Correct |
7 ms |
13524 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14484 KB |
Output is correct |
21 |
Correct |
5 ms |
8148 KB |
Output is correct |
22 |
Correct |
9 ms |
13576 KB |
Output is correct |
23 |
Correct |
7 ms |
12948 KB |
Output is correct |
24 |
Correct |
9 ms |
14100 KB |
Output is correct |
25 |
Correct |
9 ms |
14556 KB |
Output is correct |
26 |
Correct |
8 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14480 KB |
Output is correct |
28 |
Correct |
9 ms |
14548 KB |
Output is correct |
29 |
Correct |
12 ms |
14480 KB |
Output is correct |
30 |
Correct |
10 ms |
14420 KB |
Output is correct |
31 |
Correct |
9 ms |
14420 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
10 ms |
14548 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
16 ms |
13012 KB |
Output is correct |
36 |
Correct |
7 ms |
11092 KB |
Output is correct |
37 |
Correct |
23 ms |
14932 KB |
Output is correct |
38 |
Correct |
26 ms |
15456 KB |
Output is correct |
39 |
Correct |
20 ms |
15428 KB |
Output is correct |
40 |
Correct |
19 ms |
15444 KB |
Output is correct |
41 |
Correct |
21 ms |
15440 KB |
Output is correct |
42 |
Correct |
12 ms |
14896 KB |
Output is correct |
43 |
Correct |
12 ms |
14824 KB |
Output is correct |
44 |
Correct |
16 ms |
14804 KB |
Output is correct |
45 |
Correct |
30 ms |
15404 KB |
Output is correct |
46 |
Correct |
21 ms |
15372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6636 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6664 KB |
Output is correct |
7 |
Correct |
3 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6868 KB |
Output is correct |
10 |
Correct |
4 ms |
6996 KB |
Output is correct |
11 |
Correct |
4 ms |
7056 KB |
Output is correct |
12 |
Correct |
4 ms |
6996 KB |
Output is correct |
13 |
Correct |
4 ms |
6996 KB |
Output is correct |
14 |
Correct |
4 ms |
6996 KB |
Output is correct |
15 |
Correct |
6 ms |
6996 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
6 ms |
9628 KB |
Output is correct |
18 |
Correct |
7 ms |
13524 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14548 KB |
Output is correct |
21 |
Correct |
5 ms |
8148 KB |
Output is correct |
22 |
Correct |
7 ms |
13556 KB |
Output is correct |
23 |
Correct |
9 ms |
12884 KB |
Output is correct |
24 |
Correct |
9 ms |
14108 KB |
Output is correct |
25 |
Correct |
11 ms |
14484 KB |
Output is correct |
26 |
Correct |
8 ms |
14484 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
9 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14420 KB |
Output is correct |
30 |
Correct |
8 ms |
14472 KB |
Output is correct |
31 |
Correct |
8 ms |
14472 KB |
Output is correct |
32 |
Correct |
7 ms |
14516 KB |
Output is correct |
33 |
Correct |
10 ms |
14496 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
17 ms |
12884 KB |
Output is correct |
36 |
Correct |
8 ms |
10964 KB |
Output is correct |
37 |
Correct |
19 ms |
14828 KB |
Output is correct |
38 |
Correct |
21 ms |
15188 KB |
Output is correct |
39 |
Correct |
23 ms |
15256 KB |
Output is correct |
40 |
Correct |
22 ms |
15188 KB |
Output is correct |
41 |
Correct |
20 ms |
15212 KB |
Output is correct |
42 |
Correct |
11 ms |
14676 KB |
Output is correct |
43 |
Correct |
14 ms |
14708 KB |
Output is correct |
44 |
Correct |
12 ms |
14632 KB |
Output is correct |
45 |
Correct |
30 ms |
15228 KB |
Output is correct |
46 |
Correct |
21 ms |
15224 KB |
Output is correct |
47 |
Correct |
93 ms |
57156 KB |
Output is correct |
48 |
Correct |
54 ms |
99024 KB |
Output is correct |
49 |
Correct |
56 ms |
107308 KB |
Output is correct |
50 |
Correct |
60 ms |
116852 KB |
Output is correct |
51 |
Correct |
113 ms |
123912 KB |
Output is correct |
52 |
Correct |
112 ms |
123840 KB |
Output is correct |
53 |
Correct |
71 ms |
121408 KB |
Output is correct |
54 |
Correct |
53 ms |
120120 KB |
Output is correct |
55 |
Correct |
65 ms |
120152 KB |
Output is correct |
56 |
Correct |
78 ms |
121648 KB |
Output is correct |
57 |
Correct |
66 ms |
120240 KB |
Output is correct |
58 |
Correct |
65 ms |
120484 KB |
Output is correct |
59 |
Correct |
59 ms |
120576 KB |
Output is correct |
60 |
Correct |
69 ms |
120608 KB |
Output is correct |
61 |
Correct |
69 ms |
120804 KB |
Output is correct |
62 |
Correct |
69 ms |
122224 KB |
Output is correct |
63 |
Correct |
157 ms |
120956 KB |
Output is correct |
64 |
Correct |
144 ms |
120972 KB |
Output is correct |
65 |
Correct |
228 ms |
120972 KB |
Output is correct |
66 |
Correct |
172 ms |
121096 KB |
Output is correct |
67 |
Correct |
179 ms |
121160 KB |
Output is correct |