#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 3e4+10;
const int S = 100;
const int INF = 1e9;
int n, m, d[N+10];
vector<pii> G[N+10];
map<pii, vector<int>> mp;
void dijk(int v, int end) {
for (int i=0; i<n; ++i) d[i]=INF;
d[v]=0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, v});
while (!pq.empty()) {
pii x=pq.top(); pq.pop();
int v=x.second;
if (x.first > d[v]) continue;
for (auto u : G[v]) {
if (d[u.second] > u.first+d[v]) {
d[u.second]=u.first+d[v];
pq.push({d[u.second], u.second});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
int end=-1, st=-1;
for (int i=0; i<m; ++i) {
int b, p; cin>>b>>p;
mp[{p, b%p}].push_back(b);
if (i == 1) end=b;
if (i == 0) st=b;
}
if (end == st) {
cout<<0<<"\n";
return 0;
}
for (auto [x, v] : mp) {
int j=x.first;
if (v.empty()) continue;
v.push_back(0), v.push_back(n);
sort(v.begin(), v.end());
int sz=v.size();
for (int k=1; k+1<sz; ++k) {
int i=v[k]-j, ds=1;
while (i >= v[k-1]) G[v[k]].push_back({ds++, i}), i-=j;
i=v[k]+j, ds=1;
while (i <= v[k+1]) G[v[k]].push_back({ds++, i}), i+=j;
}
} dijk(st, end);
if (d[end] == INF) cout<<-1<<"\n";
else cout<<d[end]<<"\n";
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:57:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for (auto [x, v] : mp) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1300 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
2 ms |
1492 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
5 ms |
2772 KB |
Output is correct |
34 |
Correct |
5 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
0 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
2 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1364 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1492 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
2 ms |
1492 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
5 ms |
2772 KB |
Output is correct |
34 |
Correct |
5 ms |
2772 KB |
Output is correct |
35 |
Correct |
16 ms |
4768 KB |
Output is correct |
36 |
Correct |
3 ms |
1748 KB |
Output is correct |
37 |
Correct |
14 ms |
5076 KB |
Output is correct |
38 |
Correct |
19 ms |
5988 KB |
Output is correct |
39 |
Correct |
19 ms |
6084 KB |
Output is correct |
40 |
Correct |
19 ms |
5972 KB |
Output is correct |
41 |
Correct |
23 ms |
5988 KB |
Output is correct |
42 |
Correct |
4 ms |
1380 KB |
Output is correct |
43 |
Correct |
4 ms |
1364 KB |
Output is correct |
44 |
Correct |
4 ms |
1364 KB |
Output is correct |
45 |
Correct |
23 ms |
8944 KB |
Output is correct |
46 |
Correct |
23 ms |
8916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
984 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
984 KB |
Output is correct |
8 |
Correct |
1 ms |
984 KB |
Output is correct |
9 |
Correct |
1 ms |
984 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
1040 KB |
Output is correct |
14 |
Correct |
1 ms |
1240 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
3 ms |
1364 KB |
Output is correct |
29 |
Correct |
3 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
2 ms |
1500 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
5 ms |
2780 KB |
Output is correct |
34 |
Correct |
5 ms |
2772 KB |
Output is correct |
35 |
Correct |
20 ms |
4900 KB |
Output is correct |
36 |
Correct |
3 ms |
1748 KB |
Output is correct |
37 |
Correct |
13 ms |
5076 KB |
Output is correct |
38 |
Correct |
19 ms |
5988 KB |
Output is correct |
39 |
Correct |
19 ms |
6020 KB |
Output is correct |
40 |
Correct |
19 ms |
5984 KB |
Output is correct |
41 |
Correct |
19 ms |
5972 KB |
Output is correct |
42 |
Correct |
4 ms |
1364 KB |
Output is correct |
43 |
Correct |
4 ms |
1364 KB |
Output is correct |
44 |
Correct |
4 ms |
1364 KB |
Output is correct |
45 |
Correct |
23 ms |
8940 KB |
Output is correct |
46 |
Correct |
24 ms |
8900 KB |
Output is correct |
47 |
Correct |
35 ms |
12748 KB |
Output is correct |
48 |
Correct |
16 ms |
5840 KB |
Output is correct |
49 |
Correct |
14 ms |
4948 KB |
Output is correct |
50 |
Correct |
13 ms |
4388 KB |
Output is correct |
51 |
Correct |
34 ms |
8808 KB |
Output is correct |
52 |
Correct |
35 ms |
9252 KB |
Output is correct |
53 |
Correct |
23 ms |
6244 KB |
Output is correct |
54 |
Correct |
2 ms |
1620 KB |
Output is correct |
55 |
Correct |
3 ms |
2004 KB |
Output is correct |
56 |
Correct |
8 ms |
2396 KB |
Output is correct |
57 |
Correct |
26 ms |
10144 KB |
Output is correct |
58 |
Correct |
6 ms |
2392 KB |
Output is correct |
59 |
Correct |
8 ms |
2812 KB |
Output is correct |
60 |
Correct |
9 ms |
3360 KB |
Output is correct |
61 |
Correct |
9 ms |
3108 KB |
Output is correct |
62 |
Correct |
25 ms |
9704 KB |
Output is correct |
63 |
Correct |
67 ms |
28100 KB |
Output is correct |
64 |
Correct |
75 ms |
36608 KB |
Output is correct |
65 |
Correct |
104 ms |
46576 KB |
Output is correct |
66 |
Correct |
167 ms |
74240 KB |
Output is correct |
67 |
Correct |
172 ms |
74552 KB |
Output is correct |