#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 3e4 + 10;
vector<pair<int, int> > g[MAX_N];
vector<int> bucket[MAX_N];
int currentSorter = 1;
int n, m, q;
bool cmp(const int a, const int b) {
if(a % currentSorter == b % currentSorter) {
return a < b;
}
return (a % currentSorter) < (b % currentSorter);
}
void genGraph() {
for(int d = 0; d < MAX_N; d ++) {
if(bucket[d].size() == 0) {continue;}
currentSorter = d;
sort(bucket[d].begin(), bucket[d].end(), cmp);
for(int i = 0; i < bucket[d].size(); i ++) {
int j = i;
while(j + 1 < bucket[d].size() && bucket[d][j + 1] % d == bucket[d][i] % d) {
j ++;
}
int lft = i - 1;
int rght = i;
for(int k = bucket[d][i] % d; k < n; k += d) {
if(lft != i - 1) {
g[bucket[d][lft]].push_back({k, abs(bucket[d][lft] - k) / d});
}
while(rght != j + 1 && k == bucket[d][rght]) {
lft = rght;
rght ++;
}
if(rght != j + 1) {
g[bucket[d][rght]].push_back({k, abs(bucket[d][rght] - k) / d});
}
}
i = j;
}
}
}
int dist[MAX_N];
int first, second;
int dijkstra() {
for(int i = 0; i < n; i ++) {
dist[i] = mod;
}
priority_queue<pair<int, int> > pq;
pq.push({0, first});
dist[first] = 0;
while(!pq.empty()) {
auto curr = pq.top(); pq.pop();
curr.first *= -1;
for(auto it : g[curr.second]) {
if(dist[it.first] > curr.first + it.second) {
dist[it.first] = curr.first + it.second;
pq.push({-dist[it.first], it.first});
}
}
}
if(dist[second] == mod) {
return -1;
} else {
return dist[second];
}
}
signed main() {
//ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; i ++) {
int a, b;
cin >> a >> b;
if(i == 0) {
first = a;
} else if(i == 1) {
second = a;
}
bucket[b].push_back(a);
}
genGraph();
cout << dijkstra() << endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'void genGraph()':
skyscraper.cpp:32:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i = 0; i < bucket[d].size(); i ++) {
| ~~^~~~~~~~~~~~~~~~~~
skyscraper.cpp:34:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(j + 1 < bucket[d].size() && bucket[d][j + 1] % d == bucket[d][i] % d) {
| ~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
2 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1704 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1712 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1700 KB |
Output is correct |
10 |
Correct |
2 ms |
1612 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1720 KB |
Output is correct |
13 |
Correct |
2 ms |
1612 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1704 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1708 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1692 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1612 KB |
Output is correct |
10 |
Correct |
2 ms |
1708 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
2 ms |
1612 KB |
Output is correct |
13 |
Correct |
2 ms |
1612 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1724 KB |
Output is correct |
17 |
Correct |
3 ms |
1868 KB |
Output is correct |
18 |
Correct |
2 ms |
1716 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
3 ms |
1740 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
1740 KB |
Output is correct |
24 |
Correct |
3 ms |
1868 KB |
Output is correct |
25 |
Correct |
3 ms |
1868 KB |
Output is correct |
26 |
Correct |
3 ms |
1788 KB |
Output is correct |
27 |
Correct |
3 ms |
1740 KB |
Output is correct |
28 |
Correct |
4 ms |
1968 KB |
Output is correct |
29 |
Correct |
4 ms |
2380 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
3 ms |
2124 KB |
Output is correct |
32 |
Correct |
3 ms |
1960 KB |
Output is correct |
33 |
Correct |
6 ms |
3148 KB |
Output is correct |
34 |
Correct |
7 ms |
3148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1708 KB |
Output is correct |
7 |
Correct |
1 ms |
1712 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1712 KB |
Output is correct |
10 |
Correct |
2 ms |
1612 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1712 KB |
Output is correct |
13 |
Correct |
3 ms |
1712 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1740 KB |
Output is correct |
17 |
Correct |
3 ms |
1868 KB |
Output is correct |
18 |
Correct |
2 ms |
1740 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
3 ms |
1740 KB |
Output is correct |
21 |
Correct |
2 ms |
1720 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
3 ms |
1740 KB |
Output is correct |
24 |
Correct |
4 ms |
1868 KB |
Output is correct |
25 |
Correct |
3 ms |
1868 KB |
Output is correct |
26 |
Correct |
3 ms |
1740 KB |
Output is correct |
27 |
Correct |
3 ms |
1740 KB |
Output is correct |
28 |
Correct |
3 ms |
1868 KB |
Output is correct |
29 |
Correct |
4 ms |
2484 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
3 ms |
2124 KB |
Output is correct |
32 |
Correct |
3 ms |
1996 KB |
Output is correct |
33 |
Correct |
6 ms |
3148 KB |
Output is correct |
34 |
Correct |
6 ms |
3156 KB |
Output is correct |
35 |
Correct |
20 ms |
3144 KB |
Output is correct |
36 |
Correct |
5 ms |
2000 KB |
Output is correct |
37 |
Correct |
18 ms |
4044 KB |
Output is correct |
38 |
Correct |
25 ms |
3944 KB |
Output is correct |
39 |
Correct |
24 ms |
3956 KB |
Output is correct |
40 |
Correct |
25 ms |
3944 KB |
Output is correct |
41 |
Correct |
24 ms |
3920 KB |
Output is correct |
42 |
Correct |
21 ms |
2128 KB |
Output is correct |
43 |
Correct |
21 ms |
2128 KB |
Output is correct |
44 |
Correct |
22 ms |
2132 KB |
Output is correct |
45 |
Correct |
30 ms |
6556 KB |
Output is correct |
46 |
Correct |
31 ms |
6568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1616 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1616 KB |
Output is correct |
6 |
Correct |
1 ms |
1708 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
2 ms |
1616 KB |
Output is correct |
11 |
Correct |
2 ms |
1744 KB |
Output is correct |
12 |
Correct |
2 ms |
1612 KB |
Output is correct |
13 |
Correct |
2 ms |
1612 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
3 ms |
1872 KB |
Output is correct |
18 |
Correct |
2 ms |
1740 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
3 ms |
1744 KB |
Output is correct |
21 |
Correct |
2 ms |
1748 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
3 ms |
1744 KB |
Output is correct |
24 |
Correct |
4 ms |
1868 KB |
Output is correct |
25 |
Correct |
3 ms |
1872 KB |
Output is correct |
26 |
Correct |
3 ms |
1740 KB |
Output is correct |
27 |
Correct |
3 ms |
1744 KB |
Output is correct |
28 |
Correct |
4 ms |
1872 KB |
Output is correct |
29 |
Correct |
4 ms |
2392 KB |
Output is correct |
30 |
Correct |
3 ms |
2000 KB |
Output is correct |
31 |
Correct |
3 ms |
2100 KB |
Output is correct |
32 |
Correct |
3 ms |
1976 KB |
Output is correct |
33 |
Correct |
6 ms |
3148 KB |
Output is correct |
34 |
Correct |
6 ms |
3120 KB |
Output is correct |
35 |
Correct |
20 ms |
3148 KB |
Output is correct |
36 |
Correct |
5 ms |
1996 KB |
Output is correct |
37 |
Correct |
18 ms |
4044 KB |
Output is correct |
38 |
Correct |
24 ms |
3952 KB |
Output is correct |
39 |
Correct |
24 ms |
3944 KB |
Output is correct |
40 |
Correct |
24 ms |
3948 KB |
Output is correct |
41 |
Correct |
25 ms |
3916 KB |
Output is correct |
42 |
Correct |
22 ms |
2124 KB |
Output is correct |
43 |
Correct |
21 ms |
2124 KB |
Output is correct |
44 |
Correct |
21 ms |
2172 KB |
Output is correct |
45 |
Correct |
29 ms |
6600 KB |
Output is correct |
46 |
Correct |
30 ms |
6636 KB |
Output is correct |
47 |
Correct |
45 ms |
11324 KB |
Output is correct |
48 |
Correct |
24 ms |
4888 KB |
Output is correct |
49 |
Correct |
21 ms |
3948 KB |
Output is correct |
50 |
Correct |
16 ms |
3788 KB |
Output is correct |
51 |
Correct |
51 ms |
6728 KB |
Output is correct |
52 |
Correct |
52 ms |
7052 KB |
Output is correct |
53 |
Correct |
31 ms |
3976 KB |
Output is correct |
54 |
Correct |
3 ms |
2252 KB |
Output is correct |
55 |
Correct |
5 ms |
2632 KB |
Output is correct |
56 |
Correct |
30 ms |
3044 KB |
Output is correct |
57 |
Correct |
40 ms |
10696 KB |
Output is correct |
58 |
Correct |
20 ms |
2976 KB |
Output is correct |
59 |
Correct |
26 ms |
3524 KB |
Output is correct |
60 |
Correct |
28 ms |
3964 KB |
Output is correct |
61 |
Correct |
27 ms |
3392 KB |
Output is correct |
62 |
Correct |
36 ms |
7196 KB |
Output is correct |
63 |
Correct |
75 ms |
24652 KB |
Output is correct |
64 |
Correct |
82 ms |
32652 KB |
Output is correct |
65 |
Correct |
107 ms |
43176 KB |
Output is correct |
66 |
Correct |
208 ms |
69784 KB |
Output is correct |
67 |
Correct |
205 ms |
69748 KB |
Output is correct |