#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
int dp[30001];
vector<int> G[30001];
int A[30001][2];
int l[30001], r[30001];
vector<pii> H[30001];
int main(){
int n, k;
cin >> n >> k;
set<pii> M;
for(int i = 0; i < k; i++){
cin >> A[i][0] >> A[i][1];
if(i >= 2 && M.count({A[i][0], A[i][1]})){
continue;
}
M.insert({A[i][0], A[i][1]});
G[A[i][0]].push_back(i);
H[A[i][1]].push_back({A[i][0], i});
}
for(int i = 1; i <= 30000; i++){
sort(H[i].begin(), H[i].end(), [&i](pii aa, pii bb){
int a = aa.fi, b = bb.fi;
return a % i < b % i || a % i == b % i && a < b;
});
if(H[i].size() == 0) continue;
l[H[i].front().se] = H[i].front().fi % i;
for(int j = 0; j + 1 < H[i].size(); j++){
if(H[i][j].fi % i == H[i][j + 1].fi % i){
r[H[i][j].se] = H[i][j + 1].fi;
l[H[i][j + 1].se] = H[i][j].fi;
}else{
r[H[i][j].se] = n - 1;
l[H[i][j + 1].se] = H[i][j + 1].fi % i;
}
}
r[H[i].back().se] = n - 1;
}
// for(int i = 0; i < k; i++){
// cout << l[i] << ' ' << r[i] << endl;
// }
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
q.push({0, A[0][0], 1 << 30});
fill(dp, dp + n, 1 << 30);
while(q.size()){
tuple<int, int, int> qe = q.top(); q.pop();
int w = get<0>(qe), v = get<1>(qe), ban = get<2>(qe);
if(dp[v] != (1 << 30)) continue;
dp[v] = w;
for(auto i : G[v]){
for(int j = l[i]; j <= r[i]; j += A[i][1]){
int d = abs(v - j) / A[i][1];
if(dp[j] > w + d) q.push({w + d, j, i});
}
}
}
cout << (dp[A[1][0]] == (1 << 30) ? -1 : dp[A[1][0]]) << endl;
}
Compilation message
skyscraper.cpp: In lambda function:
skyscraper.cpp:28:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
28 | return a % i < b % i || a % i == b % i && a < b;
| ~~~~~~~~~~~~~~~^~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:32:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int j = 0; j + 1 < H[i].size(); j++){
| ~~~~~~^~~~~~~~~~~~~
skyscraper.cpp:51:39: warning: unused variable 'ban' [-Wunused-variable]
51 | int w = get<0>(qe), v = get<1>(qe), ban = get<2>(qe);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
# |
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 |
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 |
1876 KB |
Output is correct |
15 |
Correct |
3 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
3 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1620 KB |
Output is correct |
13 |
Correct |
3 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
3 ms |
1876 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 |
1748 KB |
Output is correct |
19 |
Correct |
2 ms |
1748 KB |
Output is correct |
20 |
Correct |
4 ms |
1876 KB |
Output is correct |
21 |
Correct |
3 ms |
1748 KB |
Output is correct |
22 |
Correct |
3 ms |
1804 KB |
Output is correct |
23 |
Correct |
3 ms |
1876 KB |
Output is correct |
24 |
Correct |
5 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 |
1876 KB |
Output is correct |
29 |
Correct |
11 ms |
2640 KB |
Output is correct |
30 |
Correct |
4 ms |
2004 KB |
Output is correct |
31 |
Correct |
6 ms |
2196 KB |
Output is correct |
32 |
Correct |
4 ms |
2004 KB |
Output is correct |
33 |
Correct |
25 ms |
3532 KB |
Output is correct |
34 |
Correct |
27 ms |
3532 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 |
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 |
1876 KB |
Output is correct |
15 |
Correct |
3 ms |
1876 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 |
1816 KB |
Output is correct |
19 |
Correct |
2 ms |
1748 KB |
Output is correct |
20 |
Correct |
4 ms |
1876 KB |
Output is correct |
21 |
Correct |
3 ms |
1748 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 KB |
Output is correct |
23 |
Correct |
4 ms |
1836 KB |
Output is correct |
24 |
Correct |
5 ms |
2004 KB |
Output is correct |
25 |
Correct |
4 ms |
1928 KB |
Output is correct |
26 |
Correct |
2 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1748 KB |
Output is correct |
28 |
Correct |
4 ms |
1876 KB |
Output is correct |
29 |
Correct |
12 ms |
2640 KB |
Output is correct |
30 |
Correct |
4 ms |
2004 KB |
Output is correct |
31 |
Correct |
6 ms |
2196 KB |
Output is correct |
32 |
Correct |
4 ms |
2004 KB |
Output is correct |
33 |
Correct |
26 ms |
3532 KB |
Output is correct |
34 |
Correct |
25 ms |
3532 KB |
Output is correct |
35 |
Correct |
36 ms |
4752 KB |
Output is correct |
36 |
Correct |
7 ms |
2260 KB |
Output is correct |
37 |
Correct |
38 ms |
4948 KB |
Output is correct |
38 |
Correct |
44 ms |
5828 KB |
Output is correct |
39 |
Correct |
47 ms |
5816 KB |
Output is correct |
40 |
Correct |
49 ms |
5780 KB |
Output is correct |
41 |
Correct |
45 ms |
5848 KB |
Output is correct |
42 |
Correct |
14 ms |
1980 KB |
Output is correct |
43 |
Correct |
14 ms |
1908 KB |
Output is correct |
44 |
Correct |
14 ms |
2492 KB |
Output is correct |
45 |
Correct |
139 ms |
10600 KB |
Output is correct |
46 |
Correct |
123 ms |
10536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
2 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 |
1760 KB |
Output is correct |
11 |
Correct |
3 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1620 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
3 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
5 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
1748 KB |
Output is correct |
19 |
Correct |
3 ms |
1748 KB |
Output is correct |
20 |
Correct |
4 ms |
1876 KB |
Output is correct |
21 |
Correct |
2 ms |
1748 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 KB |
Output is correct |
23 |
Correct |
3 ms |
1876 KB |
Output is correct |
24 |
Correct |
4 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 |
1900 KB |
Output is correct |
29 |
Correct |
11 ms |
2640 KB |
Output is correct |
30 |
Correct |
4 ms |
2068 KB |
Output is correct |
31 |
Correct |
6 ms |
2196 KB |
Output is correct |
32 |
Correct |
4 ms |
2004 KB |
Output is correct |
33 |
Correct |
24 ms |
3532 KB |
Output is correct |
34 |
Correct |
25 ms |
3532 KB |
Output is correct |
35 |
Correct |
46 ms |
4800 KB |
Output is correct |
36 |
Correct |
8 ms |
2260 KB |
Output is correct |
37 |
Correct |
47 ms |
4928 KB |
Output is correct |
38 |
Correct |
43 ms |
5836 KB |
Output is correct |
39 |
Correct |
46 ms |
5836 KB |
Output is correct |
40 |
Correct |
45 ms |
5872 KB |
Output is correct |
41 |
Correct |
48 ms |
5792 KB |
Output is correct |
42 |
Correct |
14 ms |
1876 KB |
Output is correct |
43 |
Correct |
14 ms |
2000 KB |
Output is correct |
44 |
Correct |
16 ms |
2488 KB |
Output is correct |
45 |
Correct |
126 ms |
10572 KB |
Output is correct |
46 |
Correct |
129 ms |
10544 KB |
Output is correct |
47 |
Correct |
117 ms |
10532 KB |
Output is correct |
48 |
Correct |
31 ms |
4292 KB |
Output is correct |
49 |
Correct |
31 ms |
4216 KB |
Output is correct |
50 |
Correct |
18 ms |
3552 KB |
Output is correct |
51 |
Correct |
69 ms |
6944 KB |
Output is correct |
52 |
Correct |
73 ms |
8440 KB |
Output is correct |
53 |
Correct |
52 ms |
5832 KB |
Output is correct |
54 |
Correct |
3 ms |
2104 KB |
Output is correct |
55 |
Correct |
4 ms |
2132 KB |
Output is correct |
56 |
Correct |
40 ms |
5076 KB |
Output is correct |
57 |
Correct |
86 ms |
3788 KB |
Output is correct |
58 |
Correct |
12 ms |
2468 KB |
Output is correct |
59 |
Correct |
18 ms |
2696 KB |
Output is correct |
60 |
Correct |
18 ms |
2612 KB |
Output is correct |
61 |
Correct |
23 ms |
2864 KB |
Output is correct |
62 |
Correct |
45 ms |
5180 KB |
Output is correct |
63 |
Correct |
322 ms |
16912 KB |
Output is correct |
64 |
Correct |
380 ms |
16828 KB |
Output is correct |
65 |
Execution timed out |
1002 ms |
29216 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |