#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[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;
// }
q[0].push_back(A[0][0]);
fill(dp, dp + n, 1 << 30);
for(int a = 0; a <= 30000; 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 |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
5 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
3 ms |
2516 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
3 ms |
2644 KB |
Output is correct |
21 |
Correct |
3 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
3 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
4 ms |
2644 KB |
Output is correct |
26 |
Correct |
3 ms |
2516 KB |
Output is correct |
27 |
Correct |
3 ms |
2516 KB |
Output is correct |
28 |
Correct |
4 ms |
2624 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
3 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
6 ms |
3156 KB |
Output is correct |
34 |
Correct |
6 ms |
3212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2312 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2584 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
3 ms |
2612 KB |
Output is correct |
15 |
Correct |
4 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
4 ms |
2516 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
4 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
3 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
2644 KB |
Output is correct |
26 |
Correct |
3 ms |
2520 KB |
Output is correct |
27 |
Correct |
3 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
3 ms |
2644 KB |
Output is correct |
32 |
Correct |
3 ms |
2516 KB |
Output is correct |
33 |
Correct |
6 ms |
3156 KB |
Output is correct |
34 |
Correct |
6 ms |
3156 KB |
Output is correct |
35 |
Correct |
36 ms |
4800 KB |
Output is correct |
36 |
Correct |
6 ms |
2792 KB |
Output is correct |
37 |
Correct |
26 ms |
4480 KB |
Output is correct |
38 |
Correct |
34 ms |
5188 KB |
Output is correct |
39 |
Correct |
33 ms |
5188 KB |
Output is correct |
40 |
Correct |
37 ms |
5196 KB |
Output is correct |
41 |
Correct |
38 ms |
5212 KB |
Output is correct |
42 |
Correct |
16 ms |
2644 KB |
Output is correct |
43 |
Correct |
20 ms |
2724 KB |
Output is correct |
44 |
Correct |
15 ms |
3012 KB |
Output is correct |
45 |
Correct |
44 ms |
6868 KB |
Output is correct |
46 |
Correct |
36 ms |
6860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
4 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
3 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
3 ms |
2516 KB |
Output is correct |
24 |
Correct |
4 ms |
2644 KB |
Output is correct |
25 |
Correct |
4 ms |
2644 KB |
Output is correct |
26 |
Correct |
3 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
4 ms |
2644 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
3 ms |
2656 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
5 ms |
3156 KB |
Output is correct |
34 |
Correct |
6 ms |
3156 KB |
Output is correct |
35 |
Correct |
29 ms |
4848 KB |
Output is correct |
36 |
Correct |
6 ms |
2900 KB |
Output is correct |
37 |
Correct |
25 ms |
4448 KB |
Output is correct |
38 |
Correct |
39 ms |
5280 KB |
Output is correct |
39 |
Correct |
43 ms |
5272 KB |
Output is correct |
40 |
Correct |
43 ms |
5388 KB |
Output is correct |
41 |
Correct |
37 ms |
5216 KB |
Output is correct |
42 |
Correct |
14 ms |
2656 KB |
Output is correct |
43 |
Correct |
14 ms |
2744 KB |
Output is correct |
44 |
Correct |
14 ms |
3112 KB |
Output is correct |
45 |
Correct |
40 ms |
6884 KB |
Output is correct |
46 |
Correct |
39 ms |
6976 KB |
Output is correct |
47 |
Correct |
47 ms |
6772 KB |
Output is correct |
48 |
Correct |
37 ms |
4812 KB |
Output is correct |
49 |
Correct |
26 ms |
4696 KB |
Output is correct |
50 |
Correct |
18 ms |
4140 KB |
Output is correct |
51 |
Correct |
49 ms |
6984 KB |
Output is correct |
52 |
Correct |
45 ms |
6904 KB |
Output is correct |
53 |
Runtime error |
42 ms |
12680 KB |
Execution killed with signal 6 |
54 |
Halted |
0 ms |
0 KB |
- |