#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];
vector<int> q[500001];
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;
// }
q[0].push_back(A[0][0]);
fill(dp, dp + n, 1 << 30);
for(int a = 0; a <= 500000; a++){
while(q[a].size()){
int v = q[a].back(); q[a].pop_back();
if(dp[v] != (1 << 30)) continue;
dp[v] = a;
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] > a + d) q[a + d].push_back(j);
}
}
}
}
cout << (dp[A[1][0]] == (1 << 30) ? -1 : dp[A[1][0]]) << endl;
}
Compilation message
skyscraper.cpp: In lambda function:
skyscraper.cpp:29:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
29 | return a % i < b % i || a % i == b % i && a < b;
| ~~~~~~~~~~~~~~~^~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:33: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]
33 | for(int j = 0; j + 1 < H[i].size(); j++){
| ~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
13396 KB |
Output is correct |
2 |
Correct |
9 ms |
13396 KB |
Output is correct |
3 |
Correct |
9 ms |
13396 KB |
Output is correct |
4 |
Correct |
10 ms |
13428 KB |
Output is correct |
5 |
Correct |
8 ms |
13456 KB |
Output is correct |
6 |
Correct |
9 ms |
13396 KB |
Output is correct |
7 |
Correct |
10 ms |
13396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
13396 KB |
Output is correct |
2 |
Correct |
9 ms |
13456 KB |
Output is correct |
3 |
Correct |
9 ms |
13396 KB |
Output is correct |
4 |
Correct |
8 ms |
13456 KB |
Output is correct |
5 |
Correct |
9 ms |
13380 KB |
Output is correct |
6 |
Correct |
11 ms |
13360 KB |
Output is correct |
7 |
Correct |
8 ms |
13444 KB |
Output is correct |
8 |
Correct |
9 ms |
13460 KB |
Output is correct |
9 |
Correct |
9 ms |
13396 KB |
Output is correct |
10 |
Correct |
9 ms |
13396 KB |
Output is correct |
11 |
Correct |
11 ms |
13588 KB |
Output is correct |
12 |
Correct |
14 ms |
13460 KB |
Output is correct |
13 |
Correct |
10 ms |
13464 KB |
Output is correct |
14 |
Correct |
11 ms |
13652 KB |
Output is correct |
15 |
Correct |
13 ms |
13588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
13396 KB |
Output is correct |
2 |
Correct |
8 ms |
13368 KB |
Output is correct |
3 |
Correct |
8 ms |
13452 KB |
Output is correct |
4 |
Correct |
9 ms |
13396 KB |
Output is correct |
5 |
Correct |
9 ms |
13432 KB |
Output is correct |
6 |
Correct |
10 ms |
13396 KB |
Output is correct |
7 |
Correct |
8 ms |
13460 KB |
Output is correct |
8 |
Correct |
9 ms |
13396 KB |
Output is correct |
9 |
Correct |
9 ms |
13396 KB |
Output is correct |
10 |
Correct |
10 ms |
13436 KB |
Output is correct |
11 |
Correct |
11 ms |
13524 KB |
Output is correct |
12 |
Correct |
12 ms |
13396 KB |
Output is correct |
13 |
Correct |
12 ms |
13460 KB |
Output is correct |
14 |
Correct |
10 ms |
13592 KB |
Output is correct |
15 |
Correct |
10 ms |
13652 KB |
Output is correct |
16 |
Correct |
9 ms |
13524 KB |
Output is correct |
17 |
Correct |
13 ms |
13652 KB |
Output is correct |
18 |
Correct |
10 ms |
13596 KB |
Output is correct |
19 |
Correct |
10 ms |
13524 KB |
Output is correct |
20 |
Correct |
10 ms |
13728 KB |
Output is correct |
21 |
Correct |
10 ms |
13524 KB |
Output is correct |
22 |
Correct |
10 ms |
13480 KB |
Output is correct |
23 |
Correct |
10 ms |
13584 KB |
Output is correct |
24 |
Correct |
11 ms |
13652 KB |
Output is correct |
25 |
Correct |
12 ms |
13652 KB |
Output is correct |
26 |
Correct |
13 ms |
13464 KB |
Output is correct |
27 |
Correct |
12 ms |
13652 KB |
Output is correct |
28 |
Correct |
13 ms |
13736 KB |
Output is correct |
29 |
Correct |
13 ms |
13844 KB |
Output is correct |
30 |
Correct |
10 ms |
13572 KB |
Output is correct |
31 |
Correct |
10 ms |
13652 KB |
Output is correct |
32 |
Correct |
10 ms |
13652 KB |
Output is correct |
33 |
Correct |
12 ms |
14240 KB |
Output is correct |
34 |
Correct |
12 ms |
14232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
13456 KB |
Output is correct |
2 |
Correct |
8 ms |
13396 KB |
Output is correct |
3 |
Correct |
9 ms |
13452 KB |
Output is correct |
4 |
Correct |
8 ms |
13424 KB |
Output is correct |
5 |
Correct |
8 ms |
13396 KB |
Output is correct |
6 |
Correct |
9 ms |
13456 KB |
Output is correct |
7 |
Correct |
11 ms |
13396 KB |
Output is correct |
8 |
Correct |
10 ms |
13396 KB |
Output is correct |
9 |
Correct |
9 ms |
13456 KB |
Output is correct |
10 |
Correct |
11 ms |
13380 KB |
Output is correct |
11 |
Correct |
11 ms |
13640 KB |
Output is correct |
12 |
Correct |
9 ms |
13396 KB |
Output is correct |
13 |
Correct |
9 ms |
13524 KB |
Output is correct |
14 |
Correct |
10 ms |
13656 KB |
Output is correct |
15 |
Correct |
10 ms |
13652 KB |
Output is correct |
16 |
Correct |
9 ms |
13456 KB |
Output is correct |
17 |
Correct |
11 ms |
13652 KB |
Output is correct |
18 |
Correct |
12 ms |
13588 KB |
Output is correct |
19 |
Correct |
9 ms |
13524 KB |
Output is correct |
20 |
Correct |
11 ms |
13720 KB |
Output is correct |
21 |
Correct |
10 ms |
13524 KB |
Output is correct |
22 |
Correct |
10 ms |
13524 KB |
Output is correct |
23 |
Correct |
13 ms |
13524 KB |
Output is correct |
24 |
Correct |
14 ms |
13652 KB |
Output is correct |
25 |
Correct |
14 ms |
13724 KB |
Output is correct |
26 |
Correct |
9 ms |
13464 KB |
Output is correct |
27 |
Correct |
10 ms |
13524 KB |
Output is correct |
28 |
Correct |
11 ms |
13644 KB |
Output is correct |
29 |
Correct |
11 ms |
13836 KB |
Output is correct |
30 |
Correct |
9 ms |
13524 KB |
Output is correct |
31 |
Correct |
9 ms |
13624 KB |
Output is correct |
32 |
Correct |
9 ms |
13588 KB |
Output is correct |
33 |
Correct |
12 ms |
14164 KB |
Output is correct |
34 |
Correct |
14 ms |
14164 KB |
Output is correct |
35 |
Correct |
36 ms |
16012 KB |
Output is correct |
36 |
Correct |
14 ms |
13888 KB |
Output is correct |
37 |
Correct |
32 ms |
15640 KB |
Output is correct |
38 |
Correct |
48 ms |
16592 KB |
Output is correct |
39 |
Correct |
48 ms |
16552 KB |
Output is correct |
40 |
Correct |
47 ms |
16528 KB |
Output is correct |
41 |
Correct |
43 ms |
16556 KB |
Output is correct |
42 |
Correct |
21 ms |
13908 KB |
Output is correct |
43 |
Correct |
21 ms |
13908 KB |
Output is correct |
44 |
Correct |
21 ms |
14124 KB |
Output is correct |
45 |
Correct |
45 ms |
17836 KB |
Output is correct |
46 |
Correct |
51 ms |
17964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
13396 KB |
Output is correct |
2 |
Correct |
9 ms |
13456 KB |
Output is correct |
3 |
Correct |
9 ms |
13456 KB |
Output is correct |
4 |
Correct |
10 ms |
13456 KB |
Output is correct |
5 |
Correct |
9 ms |
13436 KB |
Output is correct |
6 |
Correct |
8 ms |
13456 KB |
Output is correct |
7 |
Correct |
8 ms |
13456 KB |
Output is correct |
8 |
Correct |
8 ms |
13396 KB |
Output is correct |
9 |
Correct |
10 ms |
13460 KB |
Output is correct |
10 |
Correct |
10 ms |
13504 KB |
Output is correct |
11 |
Correct |
11 ms |
13544 KB |
Output is correct |
12 |
Correct |
10 ms |
13396 KB |
Output is correct |
13 |
Correct |
12 ms |
13444 KB |
Output is correct |
14 |
Correct |
13 ms |
13660 KB |
Output is correct |
15 |
Correct |
14 ms |
13652 KB |
Output is correct |
16 |
Correct |
11 ms |
13524 KB |
Output is correct |
17 |
Correct |
12 ms |
13592 KB |
Output is correct |
18 |
Correct |
12 ms |
13472 KB |
Output is correct |
19 |
Correct |
10 ms |
13640 KB |
Output is correct |
20 |
Correct |
11 ms |
13652 KB |
Output is correct |
21 |
Correct |
9 ms |
13592 KB |
Output is correct |
22 |
Correct |
12 ms |
13456 KB |
Output is correct |
23 |
Correct |
12 ms |
13524 KB |
Output is correct |
24 |
Correct |
13 ms |
13728 KB |
Output is correct |
25 |
Correct |
13 ms |
13648 KB |
Output is correct |
26 |
Correct |
14 ms |
13456 KB |
Output is correct |
27 |
Correct |
10 ms |
13524 KB |
Output is correct |
28 |
Correct |
11 ms |
13652 KB |
Output is correct |
29 |
Correct |
10 ms |
13908 KB |
Output is correct |
30 |
Correct |
9 ms |
13524 KB |
Output is correct |
31 |
Correct |
10 ms |
13652 KB |
Output is correct |
32 |
Correct |
10 ms |
13652 KB |
Output is correct |
33 |
Correct |
14 ms |
14164 KB |
Output is correct |
34 |
Correct |
13 ms |
14232 KB |
Output is correct |
35 |
Correct |
40 ms |
15992 KB |
Output is correct |
36 |
Correct |
15 ms |
13908 KB |
Output is correct |
37 |
Correct |
35 ms |
15672 KB |
Output is correct |
38 |
Correct |
48 ms |
16576 KB |
Output is correct |
39 |
Correct |
51 ms |
16532 KB |
Output is correct |
40 |
Correct |
42 ms |
16532 KB |
Output is correct |
41 |
Correct |
43 ms |
16496 KB |
Output is correct |
42 |
Correct |
20 ms |
13860 KB |
Output is correct |
43 |
Correct |
21 ms |
13960 KB |
Output is correct |
44 |
Correct |
23 ms |
14052 KB |
Output is correct |
45 |
Correct |
49 ms |
17864 KB |
Output is correct |
46 |
Correct |
56 ms |
17996 KB |
Output is correct |
47 |
Correct |
47 ms |
17788 KB |
Output is correct |
48 |
Correct |
35 ms |
15956 KB |
Output is correct |
49 |
Correct |
35 ms |
15796 KB |
Output is correct |
50 |
Correct |
26 ms |
15208 KB |
Output is correct |
51 |
Correct |
59 ms |
18012 KB |
Output is correct |
52 |
Correct |
58 ms |
17924 KB |
Output is correct |
53 |
Correct |
61 ms |
17224 KB |
Output is correct |
54 |
Correct |
10 ms |
14008 KB |
Output is correct |
55 |
Correct |
11 ms |
14164 KB |
Output is correct |
56 |
Correct |
48 ms |
17436 KB |
Output is correct |
57 |
Correct |
22 ms |
16384 KB |
Output is correct |
58 |
Correct |
18 ms |
14748 KB |
Output is correct |
59 |
Correct |
24 ms |
14660 KB |
Output is correct |
60 |
Correct |
24 ms |
14720 KB |
Output is correct |
61 |
Correct |
34 ms |
14948 KB |
Output is correct |
62 |
Correct |
56 ms |
17564 KB |
Output is correct |
63 |
Correct |
69 ms |
21684 KB |
Output is correct |
64 |
Correct |
68 ms |
22472 KB |
Output is correct |
65 |
Correct |
100 ms |
30896 KB |
Output is correct |
66 |
Correct |
163 ms |
49356 KB |
Output is correct |
67 |
Correct |
172 ms |
49360 KB |
Output is correct |