#include<bits/stdc++.h>
using namespace std;
const int MAXN = 30010;
vector<int> col2i[MAXN];
vector<int> ans[MAXN];
int ps[MAXN];
int x2j(int id, int x){
int p = ps[id];
return x / p;
}
int& ans_at(int i, int x){
if(i == -1)
return ans[i + 1][x];
else
return ans[i + 1][x2j(i, x)];
}
map<pair<int,int>, vector<int>> pb2bs;
int32_t main(){
cin.tie(NULL)->sync_with_stdio(false);
int n, m; cin >> n >> m;
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;
pb2bs[make_pair(p, b % p)].push_back(b);
}
ans[0] = vector<int>(n, INT_MAX);
int id = 0;
for(auto [pb, bs] : pb2bs){
auto [p, b] = pb;
ps[id] = p;
for(int j = b; j < n; j += p){
ans[id+1].push_back(INT_MAX);
}
for(auto bb : bs){
col2i[bb].push_back(id);
}
id++;
}
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 i : col2i[x]){
maybe_update(i, x, cost, 0);
}
}else{
// sou power i na coluna x
maybe_update(-1, x, cost, 0);
if(x + ps[i] < n){
maybe_update(i, x + ps[i], cost + 1, 1);
}
if(x - ps[i] >= 0){
maybe_update(i, x - ps[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:42:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | for(auto [pb, bs] : pb2bs){
| ^
skyscraper.cpp:43:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | auto [p, b] = pb;
| ^
skyscraper.cpp:96:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
96 | auto [i, x, cost] = nxt;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
2 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
4 ms |
2004 KB |
Output is correct |
15 |
Correct |
3 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
2 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
2004 KB |
Output is correct |
15 |
Correct |
3 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
4 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
1876 KB |
Output is correct |
19 |
Correct |
2 ms |
1748 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
3 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1876 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
3 ms |
2004 KB |
Output is correct |
26 |
Correct |
3 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1748 KB |
Output is correct |
28 |
Correct |
4 ms |
2132 KB |
Output is correct |
29 |
Correct |
4 ms |
2132 KB |
Output is correct |
30 |
Correct |
2 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
2004 KB |
Output is correct |
32 |
Correct |
3 ms |
1876 KB |
Output is correct |
33 |
Correct |
7 ms |
2644 KB |
Output is correct |
34 |
Correct |
8 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
2 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
2 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
2 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
4 ms |
2000 KB |
Output is correct |
15 |
Correct |
3 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
3 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
1876 KB |
Output is correct |
19 |
Correct |
2 ms |
1748 KB |
Output is correct |
20 |
Correct |
3 ms |
1760 KB |
Output is correct |
21 |
Correct |
2 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1876 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
3 ms |
2004 KB |
Output is correct |
26 |
Correct |
2 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1748 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
4 ms |
2132 KB |
Output is correct |
30 |
Correct |
2 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
1876 KB |
Output is correct |
33 |
Correct |
7 ms |
2644 KB |
Output is correct |
34 |
Correct |
7 ms |
2644 KB |
Output is correct |
35 |
Correct |
25 ms |
5972 KB |
Output is correct |
36 |
Correct |
5 ms |
2516 KB |
Output is correct |
37 |
Correct |
29 ms |
5460 KB |
Output is correct |
38 |
Correct |
30 ms |
7024 KB |
Output is correct |
39 |
Correct |
32 ms |
6960 KB |
Output is correct |
40 |
Correct |
31 ms |
6992 KB |
Output is correct |
41 |
Correct |
29 ms |
6988 KB |
Output is correct |
42 |
Correct |
6 ms |
2224 KB |
Output is correct |
43 |
Correct |
5 ms |
2260 KB |
Output is correct |
44 |
Correct |
6 ms |
2132 KB |
Output is correct |
45 |
Correct |
61 ms |
8844 KB |
Output is correct |
46 |
Correct |
53 ms |
8840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1620 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
4 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
2004 KB |
Output is correct |
15 |
Correct |
2 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
3 ms |
2024 KB |
Output is correct |
18 |
Correct |
2 ms |
1876 KB |
Output is correct |
19 |
Correct |
2 ms |
1748 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
2 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
3 ms |
1928 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
3 ms |
2004 KB |
Output is correct |
26 |
Correct |
2 ms |
1748 KB |
Output is correct |
27 |
Correct |
3 ms |
1748 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
4 ms |
2132 KB |
Output is correct |
30 |
Correct |
3 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
1876 KB |
Output is correct |
33 |
Correct |
7 ms |
2644 KB |
Output is correct |
34 |
Correct |
7 ms |
2644 KB |
Output is correct |
35 |
Correct |
26 ms |
6024 KB |
Output is correct |
36 |
Correct |
5 ms |
2516 KB |
Output is correct |
37 |
Correct |
24 ms |
5448 KB |
Output is correct |
38 |
Correct |
31 ms |
6996 KB |
Output is correct |
39 |
Correct |
34 ms |
6932 KB |
Output is correct |
40 |
Correct |
34 ms |
7032 KB |
Output is correct |
41 |
Correct |
37 ms |
7004 KB |
Output is correct |
42 |
Correct |
6 ms |
2260 KB |
Output is correct |
43 |
Correct |
5 ms |
2260 KB |
Output is correct |
44 |
Correct |
5 ms |
2132 KB |
Output is correct |
45 |
Correct |
46 ms |
8788 KB |
Output is correct |
46 |
Correct |
41 ms |
8736 KB |
Output is correct |
47 |
Correct |
84 ms |
9824 KB |
Output is correct |
48 |
Correct |
27 ms |
6484 KB |
Output is correct |
49 |
Correct |
19 ms |
5872 KB |
Output is correct |
50 |
Correct |
15 ms |
4956 KB |
Output is correct |
51 |
Correct |
49 ms |
8092 KB |
Output is correct |
52 |
Correct |
55 ms |
8444 KB |
Output is correct |
53 |
Correct |
30 ms |
7236 KB |
Output is correct |
54 |
Correct |
3 ms |
2132 KB |
Output is correct |
55 |
Correct |
4 ms |
2372 KB |
Output is correct |
56 |
Correct |
12 ms |
3796 KB |
Output is correct |
57 |
Correct |
23 ms |
4372 KB |
Output is correct |
58 |
Correct |
8 ms |
2724 KB |
Output is correct |
59 |
Correct |
10 ms |
3048 KB |
Output is correct |
60 |
Correct |
11 ms |
3160 KB |
Output is correct |
61 |
Correct |
13 ms |
3504 KB |
Output is correct |
62 |
Correct |
44 ms |
9472 KB |
Output is correct |
63 |
Correct |
133 ms |
19008 KB |
Output is correct |
64 |
Correct |
141 ms |
23052 KB |
Output is correct |
65 |
Correct |
185 ms |
28584 KB |
Output is correct |
66 |
Correct |
562 ms |
47844 KB |
Output is correct |
67 |
Correct |
429 ms |
47848 KB |
Output is correct |