#include <bits/stdc++.h>
#define CHECK cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 1e18;
const int OO = 1;
const int OOO = 1;
using namespace std;
const int lim = 1000003;
struct edge {
int to, w;
edge(int tt, int ww) {
to = tt;
w = ww;
}
bool operator < (const edge &a) const {
return w > a.w;
}
};
int n, m;
int B[30003], P[30003];
vector<edge> g[30003];
map<pair<int, int>, vector<int>> M;
int d[30003];
vector<int> nxt[lim];
void add(vector<int> &pos, int len) {
sort(pos.begin(), pos.end());
int l, r;
for (int i = 0; i < pos.size(); i++) {
l = (i == 0 ? 0 : pos[i - 1]);
r = (i == (int)pos.size() - 1 ? n - 1 : pos[i + 1]);
for (int j = pos[i] - len; j >= l; j -= len)
g[pos[i]].push_back(edge(j, (pos[i] - j) / len));
for (int j = pos[i] + len; j <= r; j += len)
g[pos[i]].push_back(edge(j, (j - pos[i]) / len));
}
}
void dijkstra() {
for (int i = 0; i < n; i++) d[i] = -1;
nxt[0].push_back(B[0]);
for (int dis = 0; d[B[1]] == -1 && dis <= lim; dis++) {
for (const auto &x : nxt[dis]) {
if (d[x] != -1) continue;
d[x] = dis;
for (const auto &i : g[x])
if (dis + i.w <= lim && d[i.to] == -1)
nxt[dis + i.w].push_back(i.to);
}
}
/*
priority_queue<edge> q;
q.push(edge(B[0], 0));
while (q.size()) {
edge x = q.top();
q.pop();
if (d[x.to] != -1) continue;
d[x.to] = x.w;
for (const auto &i : g[x.to])
if (d[i.to] == -1)
q.push(edge(i.to, x.w + i.w));
}
*/
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> B[i] >> P[i];
M[{B[i] % P[i], P[i]}].push_back(B[i]);
}
for (auto &i : M) add(i.second, i.first.second);
dijkstra();
cout << d[B[1]] << '\n';
}
Compilation message
skyscraper.cpp: In function 'void add(std::vector<int>&, int)':
skyscraper.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < pos.size(); i++) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
24568 KB |
Output is correct |
2 |
Correct |
26 ms |
24576 KB |
Output is correct |
3 |
Correct |
24 ms |
24576 KB |
Output is correct |
4 |
Correct |
28 ms |
24576 KB |
Output is correct |
5 |
Correct |
23 ms |
24576 KB |
Output is correct |
6 |
Correct |
25 ms |
24568 KB |
Output is correct |
7 |
Correct |
26 ms |
24568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
24568 KB |
Output is correct |
2 |
Correct |
26 ms |
24584 KB |
Output is correct |
3 |
Correct |
29 ms |
24568 KB |
Output is correct |
4 |
Correct |
33 ms |
24576 KB |
Output is correct |
5 |
Correct |
26 ms |
24568 KB |
Output is correct |
6 |
Correct |
26 ms |
24576 KB |
Output is correct |
7 |
Correct |
25 ms |
24576 KB |
Output is correct |
8 |
Correct |
31 ms |
24548 KB |
Output is correct |
9 |
Correct |
29 ms |
24568 KB |
Output is correct |
10 |
Correct |
28 ms |
24576 KB |
Output is correct |
11 |
Correct |
27 ms |
24800 KB |
Output is correct |
12 |
Correct |
28 ms |
24576 KB |
Output is correct |
13 |
Correct |
31 ms |
24540 KB |
Output is correct |
14 |
Correct |
28 ms |
24832 KB |
Output is correct |
15 |
Correct |
28 ms |
24824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
24576 KB |
Output is correct |
2 |
Correct |
25 ms |
24576 KB |
Output is correct |
3 |
Correct |
28 ms |
24576 KB |
Output is correct |
4 |
Correct |
29 ms |
24568 KB |
Output is correct |
5 |
Correct |
27 ms |
24576 KB |
Output is correct |
6 |
Correct |
30 ms |
24576 KB |
Output is correct |
7 |
Correct |
25 ms |
24576 KB |
Output is correct |
8 |
Correct |
24 ms |
24624 KB |
Output is correct |
9 |
Correct |
26 ms |
24576 KB |
Output is correct |
10 |
Correct |
30 ms |
24628 KB |
Output is correct |
11 |
Correct |
26 ms |
24832 KB |
Output is correct |
12 |
Correct |
26 ms |
24576 KB |
Output is correct |
13 |
Correct |
26 ms |
24568 KB |
Output is correct |
14 |
Correct |
30 ms |
24824 KB |
Output is correct |
15 |
Correct |
28 ms |
24832 KB |
Output is correct |
16 |
Correct |
32 ms |
24684 KB |
Output is correct |
17 |
Correct |
28 ms |
24960 KB |
Output is correct |
18 |
Correct |
34 ms |
24696 KB |
Output is correct |
19 |
Correct |
31 ms |
24704 KB |
Output is correct |
20 |
Correct |
26 ms |
24704 KB |
Output is correct |
21 |
Correct |
33 ms |
24768 KB |
Output is correct |
22 |
Correct |
32 ms |
24700 KB |
Output is correct |
23 |
Correct |
29 ms |
24796 KB |
Output is correct |
24 |
Correct |
34 ms |
24952 KB |
Output is correct |
25 |
Correct |
28 ms |
24860 KB |
Output is correct |
26 |
Correct |
26 ms |
24696 KB |
Output is correct |
27 |
Correct |
26 ms |
24696 KB |
Output is correct |
28 |
Correct |
29 ms |
25088 KB |
Output is correct |
29 |
Correct |
34 ms |
25600 KB |
Output is correct |
30 |
Correct |
28 ms |
24824 KB |
Output is correct |
31 |
Correct |
31 ms |
25088 KB |
Output is correct |
32 |
Correct |
28 ms |
24952 KB |
Output is correct |
33 |
Correct |
30 ms |
26496 KB |
Output is correct |
34 |
Correct |
33 ms |
26364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24576 KB |
Output is correct |
2 |
Correct |
28 ms |
24580 KB |
Output is correct |
3 |
Correct |
25 ms |
24568 KB |
Output is correct |
4 |
Correct |
32 ms |
24576 KB |
Output is correct |
5 |
Correct |
25 ms |
24576 KB |
Output is correct |
6 |
Correct |
28 ms |
24576 KB |
Output is correct |
7 |
Correct |
24 ms |
24576 KB |
Output is correct |
8 |
Correct |
24 ms |
24576 KB |
Output is correct |
9 |
Correct |
26 ms |
24568 KB |
Output is correct |
10 |
Correct |
25 ms |
24576 KB |
Output is correct |
11 |
Correct |
27 ms |
24700 KB |
Output is correct |
12 |
Correct |
23 ms |
24576 KB |
Output is correct |
13 |
Correct |
27 ms |
24576 KB |
Output is correct |
14 |
Correct |
27 ms |
24832 KB |
Output is correct |
15 |
Correct |
31 ms |
24832 KB |
Output is correct |
16 |
Correct |
30 ms |
24696 KB |
Output is correct |
17 |
Correct |
28 ms |
24960 KB |
Output is correct |
18 |
Correct |
30 ms |
24704 KB |
Output is correct |
19 |
Correct |
30 ms |
24700 KB |
Output is correct |
20 |
Correct |
28 ms |
24704 KB |
Output is correct |
21 |
Correct |
28 ms |
24704 KB |
Output is correct |
22 |
Correct |
30 ms |
24704 KB |
Output is correct |
23 |
Correct |
26 ms |
24828 KB |
Output is correct |
24 |
Correct |
29 ms |
24952 KB |
Output is correct |
25 |
Correct |
28 ms |
24960 KB |
Output is correct |
26 |
Correct |
25 ms |
24704 KB |
Output is correct |
27 |
Correct |
25 ms |
24704 KB |
Output is correct |
28 |
Correct |
32 ms |
25088 KB |
Output is correct |
29 |
Correct |
29 ms |
25600 KB |
Output is correct |
30 |
Correct |
26 ms |
24960 KB |
Output is correct |
31 |
Correct |
34 ms |
25080 KB |
Output is correct |
32 |
Correct |
30 ms |
24960 KB |
Output is correct |
33 |
Correct |
29 ms |
26496 KB |
Output is correct |
34 |
Correct |
37 ms |
26488 KB |
Output is correct |
35 |
Correct |
62 ms |
28792 KB |
Output is correct |
36 |
Correct |
31 ms |
25344 KB |
Output is correct |
37 |
Correct |
55 ms |
29176 KB |
Output is correct |
38 |
Correct |
74 ms |
30072 KB |
Output is correct |
39 |
Correct |
64 ms |
29816 KB |
Output is correct |
40 |
Correct |
75 ms |
29816 KB |
Output is correct |
41 |
Correct |
86 ms |
30084 KB |
Output is correct |
42 |
Correct |
32 ms |
25088 KB |
Output is correct |
43 |
Correct |
33 ms |
25080 KB |
Output is correct |
44 |
Correct |
35 ms |
25088 KB |
Output is correct |
45 |
Correct |
66 ms |
34680 KB |
Output is correct |
46 |
Correct |
75 ms |
34680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24548 KB |
Output is correct |
2 |
Correct |
25 ms |
24576 KB |
Output is correct |
3 |
Correct |
26 ms |
24576 KB |
Output is correct |
4 |
Correct |
29 ms |
24576 KB |
Output is correct |
5 |
Correct |
26 ms |
24696 KB |
Output is correct |
6 |
Correct |
27 ms |
24576 KB |
Output is correct |
7 |
Correct |
28 ms |
24568 KB |
Output is correct |
8 |
Correct |
28 ms |
24576 KB |
Output is correct |
9 |
Correct |
28 ms |
24576 KB |
Output is correct |
10 |
Correct |
53 ms |
24684 KB |
Output is correct |
11 |
Correct |
24 ms |
24704 KB |
Output is correct |
12 |
Correct |
26 ms |
24708 KB |
Output is correct |
13 |
Correct |
26 ms |
24576 KB |
Output is correct |
14 |
Correct |
27 ms |
24832 KB |
Output is correct |
15 |
Correct |
32 ms |
25016 KB |
Output is correct |
16 |
Correct |
29 ms |
24704 KB |
Output is correct |
17 |
Correct |
27 ms |
24960 KB |
Output is correct |
18 |
Correct |
30 ms |
24704 KB |
Output is correct |
19 |
Correct |
29 ms |
24696 KB |
Output is correct |
20 |
Correct |
25 ms |
24704 KB |
Output is correct |
21 |
Correct |
34 ms |
24696 KB |
Output is correct |
22 |
Correct |
32 ms |
24696 KB |
Output is correct |
23 |
Correct |
27 ms |
24704 KB |
Output is correct |
24 |
Correct |
28 ms |
24960 KB |
Output is correct |
25 |
Correct |
30 ms |
24960 KB |
Output is correct |
26 |
Correct |
27 ms |
24704 KB |
Output is correct |
27 |
Correct |
26 ms |
24704 KB |
Output is correct |
28 |
Correct |
30 ms |
25080 KB |
Output is correct |
29 |
Correct |
30 ms |
25724 KB |
Output is correct |
30 |
Correct |
50 ms |
24832 KB |
Output is correct |
31 |
Correct |
27 ms |
25088 KB |
Output is correct |
32 |
Correct |
27 ms |
24960 KB |
Output is correct |
33 |
Correct |
34 ms |
26492 KB |
Output is correct |
34 |
Correct |
29 ms |
26616 KB |
Output is correct |
35 |
Correct |
56 ms |
28664 KB |
Output is correct |
36 |
Correct |
28 ms |
25232 KB |
Output is correct |
37 |
Correct |
53 ms |
29088 KB |
Output is correct |
38 |
Correct |
71 ms |
30012 KB |
Output is correct |
39 |
Correct |
75 ms |
29688 KB |
Output is correct |
40 |
Correct |
72 ms |
29724 KB |
Output is correct |
41 |
Correct |
71 ms |
30072 KB |
Output is correct |
42 |
Correct |
32 ms |
25088 KB |
Output is correct |
43 |
Correct |
32 ms |
25088 KB |
Output is correct |
44 |
Correct |
30 ms |
25088 KB |
Output is correct |
45 |
Correct |
73 ms |
34680 KB |
Output is correct |
46 |
Correct |
71 ms |
34700 KB |
Output is correct |
47 |
Correct |
88 ms |
37368 KB |
Output is correct |
48 |
Correct |
60 ms |
29684 KB |
Output is correct |
49 |
Correct |
54 ms |
28664 KB |
Output is correct |
50 |
Correct |
46 ms |
27904 KB |
Output is correct |
51 |
Correct |
83 ms |
32884 KB |
Output is correct |
52 |
Correct |
88 ms |
33204 KB |
Output is correct |
53 |
Correct |
77 ms |
30036 KB |
Output is correct |
54 |
Correct |
32 ms |
25376 KB |
Output is correct |
55 |
Correct |
33 ms |
25980 KB |
Output is correct |
56 |
Correct |
38 ms |
26852 KB |
Output is correct |
57 |
Correct |
55 ms |
33400 KB |
Output is correct |
58 |
Correct |
44 ms |
26736 KB |
Output is correct |
59 |
Correct |
73 ms |
26996 KB |
Output is correct |
60 |
Correct |
38 ms |
27644 KB |
Output is correct |
61 |
Correct |
44 ms |
27252 KB |
Output is correct |
62 |
Correct |
81 ms |
34040 KB |
Output is correct |
63 |
Correct |
112 ms |
53868 KB |
Output is correct |
64 |
Correct |
131 ms |
57972 KB |
Output is correct |
65 |
Correct |
174 ms |
75972 KB |
Output is correct |
66 |
Correct |
356 ms |
121144 KB |
Output is correct |
67 |
Correct |
341 ms |
121496 KB |
Output is correct |