#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MAXN = 3e4;
const int MAX = 5e5;
vector<pair<int,int>> power[MAXN+1]; //power[i] saves the doges with the power i
int l[MAXN+1], r[MAXN+1]; //do it for the index
vector<int> adj[MAXN+1];
vector<int> q[MAX+1];
int dp[MAXN+1];
int val;
bool cmp(pair<int,int> a, pair<int,int> b){
if(a.f % val != b.f % val) return a.f%val < b.f % val;
return a.f < b.f;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N,M; cin >> N >> M; //number of skyscrapers, number of doges
vector<pair<int,int>> doges(M);
set<pair<int,int>> uni;
for(int i = 0;i<M;++i){
cin >> doges[i].f >> doges[i].s;
if(uni.find({doges[i].f,doges[i].s}) != uni.end()){
continue;
}
uni.insert({doges[i].f,doges[i].s});
power[doges[i].s].push_back({doges[i].f,i}); //save the ones with the same power together -> building and index
adj[doges[i].f].push_back(i);
}
for(int i = 1;i<=MAXN;++i){
if(power[i].size() == 0) continue;
val = i;
sort(power[i].begin(),power[i].end(),cmp);
l[power[i][0].s] = power[i][0].f%i;
for(int j = 0;j<power[i].size()-1;++j){
if(power[i][j].f % i == power[i][j+1].f % i){
r[power[i][j].s] = power[i][j+1].f;
l[power[i][j+1].s] = power[i][j].f;
}
else{
r[power[i][j].s] = N-1;
l[power[i][j+1].s] = power[i][j+1].f%i;
}
}
r[power[i].back().s] = N-1;
}
q[0].push_back(doges[0].f);
fill(dp,dp+N,(1<<30));
for(int i = 0;i<=MAX;++i){
while(!q[i].empty()){
int u = q[i].back(); q[i].pop_back();
if(dp[u] != (1<<30)) continue;
dp[u] = i;
for(auto v : adj[u]){
for(int j = l[v];j <= r[v];j += doges[v].s){
int cur = abs(u-j)/doges[v].s;
if(cur+i < dp[j]){
q[cur+i].push_back(j);
}
}
}
}
}
if(dp[doges[1].f] == (1<<30)){
cout << -1 << "\n";
}
else{
cout << dp[doges[1].f] << "\n";
}
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:37: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]
37 | for(int j = 0;j<power[i].size()-1;++j){
| ~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
3 ms |
13400 KB |
Output is correct |
6 |
Correct |
2 ms |
13620 KB |
Output is correct |
7 |
Correct |
2 ms |
13572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13404 KB |
Output is correct |
2 |
Correct |
2 ms |
13580 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13576 KB |
Output is correct |
5 |
Correct |
2 ms |
13404 KB |
Output is correct |
6 |
Correct |
2 ms |
13404 KB |
Output is correct |
7 |
Correct |
3 ms |
13400 KB |
Output is correct |
8 |
Correct |
2 ms |
13404 KB |
Output is correct |
9 |
Correct |
3 ms |
13680 KB |
Output is correct |
10 |
Correct |
3 ms |
13656 KB |
Output is correct |
11 |
Correct |
3 ms |
13660 KB |
Output is correct |
12 |
Correct |
2 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13660 KB |
Output is correct |
14 |
Correct |
3 ms |
13660 KB |
Output is correct |
15 |
Correct |
3 ms |
13660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13404 KB |
Output is correct |
2 |
Correct |
3 ms |
13404 KB |
Output is correct |
3 |
Correct |
3 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
2 ms |
13620 KB |
Output is correct |
6 |
Correct |
2 ms |
13404 KB |
Output is correct |
7 |
Correct |
2 ms |
13436 KB |
Output is correct |
8 |
Correct |
3 ms |
13404 KB |
Output is correct |
9 |
Correct |
2 ms |
13584 KB |
Output is correct |
10 |
Correct |
2 ms |
13660 KB |
Output is correct |
11 |
Correct |
3 ms |
13784 KB |
Output is correct |
12 |
Correct |
2 ms |
13592 KB |
Output is correct |
13 |
Correct |
3 ms |
13660 KB |
Output is correct |
14 |
Correct |
3 ms |
13660 KB |
Output is correct |
15 |
Correct |
3 ms |
13660 KB |
Output is correct |
16 |
Correct |
3 ms |
13660 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
3 ms |
13660 KB |
Output is correct |
19 |
Correct |
2 ms |
13696 KB |
Output is correct |
20 |
Correct |
3 ms |
13660 KB |
Output is correct |
21 |
Correct |
3 ms |
13656 KB |
Output is correct |
22 |
Correct |
2 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
4 ms |
13660 KB |
Output is correct |
26 |
Correct |
3 ms |
13568 KB |
Output is correct |
27 |
Correct |
3 ms |
13660 KB |
Output is correct |
28 |
Correct |
4 ms |
13848 KB |
Output is correct |
29 |
Correct |
3 ms |
13916 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13660 KB |
Output is correct |
32 |
Correct |
3 ms |
13660 KB |
Output is correct |
33 |
Correct |
4 ms |
14424 KB |
Output is correct |
34 |
Correct |
4 ms |
14172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13400 KB |
Output is correct |
2 |
Correct |
3 ms |
13404 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
3 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13400 KB |
Output is correct |
7 |
Correct |
3 ms |
13404 KB |
Output is correct |
8 |
Correct |
2 ms |
13624 KB |
Output is correct |
9 |
Correct |
2 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13660 KB |
Output is correct |
11 |
Correct |
4 ms |
13660 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13668 KB |
Output is correct |
14 |
Correct |
3 ms |
13660 KB |
Output is correct |
15 |
Correct |
3 ms |
13660 KB |
Output is correct |
16 |
Correct |
3 ms |
13660 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
3 ms |
13660 KB |
Output is correct |
19 |
Correct |
3 ms |
13660 KB |
Output is correct |
20 |
Correct |
3 ms |
13660 KB |
Output is correct |
21 |
Correct |
3 ms |
13572 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
2 ms |
13660 KB |
Output is correct |
27 |
Correct |
3 ms |
13660 KB |
Output is correct |
28 |
Correct |
4 ms |
13660 KB |
Output is correct |
29 |
Correct |
3 ms |
13916 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13656 KB |
Output is correct |
32 |
Correct |
2 ms |
13660 KB |
Output is correct |
33 |
Correct |
4 ms |
14172 KB |
Output is correct |
34 |
Correct |
4 ms |
14172 KB |
Output is correct |
35 |
Correct |
14 ms |
15888 KB |
Output is correct |
36 |
Correct |
5 ms |
14028 KB |
Output is correct |
37 |
Correct |
12 ms |
15500 KB |
Output is correct |
38 |
Correct |
18 ms |
16372 KB |
Output is correct |
39 |
Correct |
18 ms |
16476 KB |
Output is correct |
40 |
Correct |
19 ms |
16476 KB |
Output is correct |
41 |
Correct |
27 ms |
16400 KB |
Output is correct |
42 |
Correct |
5 ms |
13916 KB |
Output is correct |
43 |
Correct |
5 ms |
14172 KB |
Output is correct |
44 |
Correct |
5 ms |
14360 KB |
Output is correct |
45 |
Correct |
19 ms |
18264 KB |
Output is correct |
46 |
Correct |
19 ms |
18264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13400 KB |
Output is correct |
2 |
Correct |
2 ms |
13404 KB |
Output is correct |
3 |
Correct |
3 ms |
13404 KB |
Output is correct |
4 |
Correct |
2 ms |
13404 KB |
Output is correct |
5 |
Correct |
3 ms |
13400 KB |
Output is correct |
6 |
Correct |
2 ms |
13404 KB |
Output is correct |
7 |
Correct |
2 ms |
13404 KB |
Output is correct |
8 |
Correct |
2 ms |
13404 KB |
Output is correct |
9 |
Correct |
2 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13660 KB |
Output is correct |
11 |
Correct |
3 ms |
13660 KB |
Output is correct |
12 |
Correct |
2 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13660 KB |
Output is correct |
14 |
Correct |
3 ms |
13800 KB |
Output is correct |
15 |
Correct |
3 ms |
13576 KB |
Output is correct |
16 |
Correct |
3 ms |
13660 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
3 ms |
13912 KB |
Output is correct |
19 |
Correct |
3 ms |
13660 KB |
Output is correct |
20 |
Correct |
4 ms |
13660 KB |
Output is correct |
21 |
Correct |
3 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13656 KB |
Output is correct |
23 |
Correct |
3 ms |
13660 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
4 ms |
13660 KB |
Output is correct |
26 |
Correct |
3 ms |
13660 KB |
Output is correct |
27 |
Correct |
3 ms |
13572 KB |
Output is correct |
28 |
Correct |
3 ms |
13660 KB |
Output is correct |
29 |
Correct |
3 ms |
13916 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13660 KB |
Output is correct |
32 |
Correct |
3 ms |
13660 KB |
Output is correct |
33 |
Correct |
4 ms |
14172 KB |
Output is correct |
34 |
Correct |
4 ms |
14348 KB |
Output is correct |
35 |
Correct |
15 ms |
16008 KB |
Output is correct |
36 |
Correct |
5 ms |
13912 KB |
Output is correct |
37 |
Correct |
13 ms |
15708 KB |
Output is correct |
38 |
Correct |
18 ms |
16476 KB |
Output is correct |
39 |
Correct |
17 ms |
16476 KB |
Output is correct |
40 |
Correct |
18 ms |
16476 KB |
Output is correct |
41 |
Correct |
17 ms |
16476 KB |
Output is correct |
42 |
Correct |
5 ms |
13916 KB |
Output is correct |
43 |
Correct |
5 ms |
14172 KB |
Output is correct |
44 |
Correct |
5 ms |
14544 KB |
Output is correct |
45 |
Correct |
17 ms |
18080 KB |
Output is correct |
46 |
Correct |
18 ms |
18268 KB |
Output is correct |
47 |
Correct |
21 ms |
18012 KB |
Output is correct |
48 |
Correct |
15 ms |
16204 KB |
Output is correct |
49 |
Correct |
14 ms |
16220 KB |
Output is correct |
50 |
Correct |
10 ms |
15448 KB |
Output is correct |
51 |
Correct |
24 ms |
18376 KB |
Output is correct |
52 |
Correct |
24 ms |
18268 KB |
Output is correct |
53 |
Correct |
19 ms |
17792 KB |
Output is correct |
54 |
Correct |
4 ms |
14168 KB |
Output is correct |
55 |
Correct |
4 ms |
14172 KB |
Output is correct |
56 |
Correct |
20 ms |
17700 KB |
Output is correct |
57 |
Correct |
9 ms |
16476 KB |
Output is correct |
58 |
Correct |
5 ms |
14940 KB |
Output is correct |
59 |
Correct |
6 ms |
14936 KB |
Output is correct |
60 |
Correct |
7 ms |
14936 KB |
Output is correct |
61 |
Correct |
7 ms |
15372 KB |
Output is correct |
62 |
Correct |
19 ms |
18080 KB |
Output is correct |
63 |
Correct |
25 ms |
22104 KB |
Output is correct |
64 |
Correct |
27 ms |
22712 KB |
Output is correct |
65 |
Correct |
34 ms |
31312 KB |
Output is correct |
66 |
Correct |
57 ms |
49492 KB |
Output is correct |
67 |
Correct |
57 ms |
49188 KB |
Output is correct |