#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef unordered_set<int> uset;
const int inf = numeric_limits<int>::max();
const int P = 30005;
uset vis[P];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vi b(m), p(m), dist(n, inf);
set<pii> cnt;
vvi doge(P);
vector<vector<pii>> pos(n);
for(int i = 0; i < m; i++){
cin >> b[i] >> p[i];
if(cnt.count({b[i], p[i]})) continue;
doge[p[i]].push_back(b[i]);
cnt.insert({b[i], p[i]});
vis[p[i]].insert(b[i]);
}
for(int i = 0; i < P; i++)
sort(doge[i].begin(), doge[i].end());
for(int i = 0; i < n; i++)
for(int j = 0; j < doge[i].size(); j++)
pos[doge[i][j]].push_back({i, j});
dist[b[0]] = 0;
set<pii> s;
s.insert({0, b[0]});
while(!s.empty()){
int curr = s.begin()->se;
s.erase(s.begin());
for(auto id : pos[curr]){
int hi, lo;
hi = n - 1; // (id.se == doge[id.fi].size() - 1) ? n - 1 : doge[id.fi][id.se + 1];
lo = 0; // (id.se == 0) ? 0 : doge[id.fi][id.se - 1];
for(int i = curr, j = 0; i <= hi; i += id.fi, j++){
if(dist[i] > dist[curr] + j){
s.erase({dist[i], i});
dist[i] = dist[curr] + j;
s.insert({dist[i], i});
}
if(i != curr && vis[id.fi].count(i)) break;
}
for(int i = curr, j = 0; i >= lo; i -= id.fi, j++){
if(dist[i] > dist[curr] + j){
s.erase({dist[i], i});
dist[i] = dist[curr] + j;
s.insert({dist[i], i});
}
if(i != curr && vis[id.fi].count(i)) break;
}
}
}
cout << (dist[b[1]] == inf ? - 1 : dist[b[1]]) << endl;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:40:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < doge[i].size(); j++)
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
5 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
5 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2708 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
6 ms |
2936 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
6 ms |
2936 KB |
Output is correct |
15 |
Correct |
6 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
5 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
5 ms |
2680 KB |
Output is correct |
9 |
Correct |
5 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2792 KB |
Output is correct |
11 |
Correct |
7 ms |
2936 KB |
Output is correct |
12 |
Correct |
6 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
6 ms |
2936 KB |
Output is correct |
15 |
Correct |
6 ms |
2936 KB |
Output is correct |
16 |
Correct |
5 ms |
2808 KB |
Output is correct |
17 |
Correct |
7 ms |
3064 KB |
Output is correct |
18 |
Correct |
6 ms |
2936 KB |
Output is correct |
19 |
Correct |
6 ms |
2936 KB |
Output is correct |
20 |
Correct |
6 ms |
3092 KB |
Output is correct |
21 |
Correct |
6 ms |
2936 KB |
Output is correct |
22 |
Correct |
5 ms |
2936 KB |
Output is correct |
23 |
Correct |
6 ms |
2936 KB |
Output is correct |
24 |
Correct |
8 ms |
3064 KB |
Output is correct |
25 |
Correct |
8 ms |
3192 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2936 KB |
Output is correct |
28 |
Correct |
7 ms |
3064 KB |
Output is correct |
29 |
Correct |
10 ms |
2936 KB |
Output is correct |
30 |
Correct |
6 ms |
2808 KB |
Output is correct |
31 |
Correct |
7 ms |
2936 KB |
Output is correct |
32 |
Correct |
6 ms |
2936 KB |
Output is correct |
33 |
Correct |
15 ms |
3064 KB |
Output is correct |
34 |
Correct |
15 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2684 KB |
Output is correct |
10 |
Correct |
5 ms |
2684 KB |
Output is correct |
11 |
Correct |
6 ms |
2936 KB |
Output is correct |
12 |
Correct |
4 ms |
2780 KB |
Output is correct |
13 |
Correct |
5 ms |
2812 KB |
Output is correct |
14 |
Correct |
6 ms |
2936 KB |
Output is correct |
15 |
Correct |
6 ms |
2936 KB |
Output is correct |
16 |
Correct |
5 ms |
2808 KB |
Output is correct |
17 |
Correct |
7 ms |
2936 KB |
Output is correct |
18 |
Correct |
5 ms |
2936 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
6 ms |
3064 KB |
Output is correct |
21 |
Correct |
5 ms |
2908 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
6 ms |
2936 KB |
Output is correct |
24 |
Correct |
8 ms |
3064 KB |
Output is correct |
25 |
Correct |
8 ms |
3192 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
3108 KB |
Output is correct |
28 |
Correct |
7 ms |
3064 KB |
Output is correct |
29 |
Correct |
10 ms |
2936 KB |
Output is correct |
30 |
Correct |
7 ms |
2936 KB |
Output is correct |
31 |
Correct |
7 ms |
2936 KB |
Output is correct |
32 |
Correct |
7 ms |
2936 KB |
Output is correct |
33 |
Correct |
15 ms |
3064 KB |
Output is correct |
34 |
Correct |
15 ms |
3064 KB |
Output is correct |
35 |
Correct |
39 ms |
6008 KB |
Output is correct |
36 |
Correct |
10 ms |
3448 KB |
Output is correct |
37 |
Correct |
35 ms |
5340 KB |
Output is correct |
38 |
Correct |
50 ms |
6648 KB |
Output is correct |
39 |
Correct |
51 ms |
6652 KB |
Output is correct |
40 |
Correct |
49 ms |
6648 KB |
Output is correct |
41 |
Correct |
50 ms |
6648 KB |
Output is correct |
42 |
Correct |
10 ms |
3320 KB |
Output is correct |
43 |
Correct |
10 ms |
3320 KB |
Output is correct |
44 |
Correct |
11 ms |
3452 KB |
Output is correct |
45 |
Correct |
62 ms |
6520 KB |
Output is correct |
46 |
Correct |
60 ms |
6520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2684 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
6 ms |
2936 KB |
Output is correct |
12 |
Correct |
4 ms |
2684 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
6 ms |
2936 KB |
Output is correct |
15 |
Correct |
6 ms |
2936 KB |
Output is correct |
16 |
Correct |
5 ms |
2808 KB |
Output is correct |
17 |
Correct |
7 ms |
2936 KB |
Output is correct |
18 |
Correct |
5 ms |
2936 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
6 ms |
3068 KB |
Output is correct |
21 |
Correct |
5 ms |
2936 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
6 ms |
2936 KB |
Output is correct |
24 |
Correct |
8 ms |
3064 KB |
Output is correct |
25 |
Correct |
8 ms |
3064 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
7 ms |
3064 KB |
Output is correct |
29 |
Correct |
10 ms |
2936 KB |
Output is correct |
30 |
Correct |
6 ms |
2936 KB |
Output is correct |
31 |
Correct |
7 ms |
2936 KB |
Output is correct |
32 |
Correct |
6 ms |
2936 KB |
Output is correct |
33 |
Correct |
15 ms |
3064 KB |
Output is correct |
34 |
Correct |
15 ms |
3064 KB |
Output is correct |
35 |
Correct |
39 ms |
6008 KB |
Output is correct |
36 |
Correct |
10 ms |
3320 KB |
Output is correct |
37 |
Correct |
36 ms |
5368 KB |
Output is correct |
38 |
Correct |
50 ms |
6776 KB |
Output is correct |
39 |
Correct |
49 ms |
6648 KB |
Output is correct |
40 |
Correct |
48 ms |
6612 KB |
Output is correct |
41 |
Correct |
50 ms |
6648 KB |
Output is correct |
42 |
Correct |
10 ms |
3320 KB |
Output is correct |
43 |
Correct |
10 ms |
3320 KB |
Output is correct |
44 |
Correct |
11 ms |
3448 KB |
Output is correct |
45 |
Correct |
71 ms |
6512 KB |
Output is correct |
46 |
Correct |
64 ms |
6536 KB |
Output is correct |
47 |
Correct |
99 ms |
6776 KB |
Output is correct |
48 |
Correct |
39 ms |
7028 KB |
Output is correct |
49 |
Correct |
36 ms |
6776 KB |
Output is correct |
50 |
Correct |
26 ms |
6008 KB |
Output is correct |
51 |
Correct |
119 ms |
9720 KB |
Output is correct |
52 |
Correct |
123 ms |
9624 KB |
Output is correct |
53 |
Correct |
68 ms |
9044 KB |
Output is correct |
54 |
Correct |
10 ms |
4216 KB |
Output is correct |
55 |
Correct |
11 ms |
4216 KB |
Output is correct |
56 |
Correct |
47 ms |
7848 KB |
Output is correct |
57 |
Correct |
91 ms |
4812 KB |
Output is correct |
58 |
Correct |
18 ms |
4600 KB |
Output is correct |
59 |
Correct |
19 ms |
4728 KB |
Output is correct |
60 |
Correct |
21 ms |
4728 KB |
Output is correct |
61 |
Correct |
22 ms |
5240 KB |
Output is correct |
62 |
Correct |
65 ms |
7972 KB |
Output is correct |
63 |
Correct |
237 ms |
8696 KB |
Output is correct |
64 |
Correct |
253 ms |
8696 KB |
Output is correct |
65 |
Correct |
307 ms |
8824 KB |
Output is correct |
66 |
Correct |
492 ms |
8696 KB |
Output is correct |
67 |
Correct |
499 ms |
8568 KB |
Output is correct |