#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define MAX 40100
#define MAXS 20
#define INF 1010101010
#define bb ' '
#define ln '\n'
#define Ln '\n'
vector<pair<short, short>> adj[MAX];
int B[MAX];
int P[MAX];
vector<int> locset;
vector<int> locs[MAX];
int N, M;
int findpv(int x) {
int ind = upper_bound(locset.begin(), locset.end(), x) - locset.begin();
ind--;
if (ind < 0) return -1;
else return locset[ind];
}
int findnx(int x) {
int ind = lower_bound(locset.begin(), locset.end(), x) - locset.begin();
if (ind >= locset.size()) return -1;
else return locset[ind];
}
int np(int x, int p) { return x / p * p + p; }
int dist[MAX];
int vis[MAX];
vector<int> locp[MAX];
bool chk(vector<int>& v, int p) {
int ind = lower_bound(v.begin(), v.end(), p) - v.begin();
if (ind >= v.size() || v[ind] != p) return false;
return true;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> N >> M;
int i;
for (i = 0; i < M; i++) cin >> B[i] >> P[i], locset.emplace_back(B[i]), locs[B[i]].push_back(i), locp[B[i]].push_back(P[i]);
for (i = 0; i < N; i++) sort(locp[i].begin(), locp[i].end());
sort(locset.begin(), locset.end());
locset.erase(unique(locset.begin(), locset.end()), locset.end());
int j;
for (i = 0; i < N; i++) {
for (j = 1; j < locs[i].size(); j++) {
adj[locs[i][j]].emplace_back(locs[i][j - 1], 0);
adj[locs[i][j - 1]].emplace_back(locs[i][j], 0);
}
}
for (i = 0; i < M; i++) {
int delta = 1;
while (1) {
int loc = findpv(B[i] - delta);
if (loc == -1) break;
bool found = false;
if ((B[i] - loc) % P[i] == 0) {
if (chk(locp[loc], P[i])) {
adj[i].emplace_back(locs[loc][0], (B[i] - loc) / P[i]);
found = true;
}
else { for (auto v : locs[loc]) adj[i].emplace_back(v, (B[i] - loc) / P[i]); }
}
if (found) break;
delta = B[i] - loc;
delta = np(delta, P[i]);
}
delta = 1;
while (1) {
int loc = findnx(B[i] + delta);
if (loc == -1) break;
bool found = false;
if ((loc - B[i]) % P[i] == 0) {
if (chk(locp[loc], P[i])) {
adj[i].emplace_back(locs[loc][0], (loc - B[i]) / P[i]);
found = true;
}
else { for (auto v : locs[loc]) adj[i].emplace_back(v, (loc - B[i]) / P[i]); }
}
if (found) break;
delta = loc - B[i];
delta = np(delta, P[i]);
}
}
for (i = 1; i < M; i++) dist[i] = INF;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.emplace(0, 0);
while (pq.size()) {
int v = pq.top().second;
pq.pop();
if (vis[v]) continue;
vis[v] = 1;
for (auto& [x, c] : adj[v]) {
if (vis[x]) continue;
if (dist[x] > dist[v] + c) {
dist[x] = dist[v] + c;
pq.emplace(dist[x], x);
}
}
}
if (dist[1] >= INF) dist[1] = -1;
cout << dist[1] << Ln;
}
Compilation message
skyscraper.cpp: In function 'int findnx(int)':
skyscraper.cpp:31:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if (ind >= locset.size()) return -1;
| ~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'bool chk(std::vector<int>&, int)':
skyscraper.cpp:40:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | if (ind >= v.size() || v[ind] != p) return false;
| ~~~~^~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:53:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (j = 1; j < locs[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3168 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
8 |
Correct |
2 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
4 ms |
3668 KB |
Output is correct |
12 |
Correct |
3 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
4 ms |
3540 KB |
Output is correct |
15 |
Correct |
4 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
8 |
Correct |
2 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3056 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
4 ms |
3668 KB |
Output is correct |
12 |
Correct |
3 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
4 ms |
3540 KB |
Output is correct |
15 |
Correct |
4 ms |
3540 KB |
Output is correct |
16 |
Correct |
3 ms |
3284 KB |
Output is correct |
17 |
Correct |
6 ms |
3480 KB |
Output is correct |
18 |
Correct |
3 ms |
3156 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
3 ms |
3156 KB |
Output is correct |
22 |
Correct |
3 ms |
3156 KB |
Output is correct |
23 |
Correct |
3 ms |
3156 KB |
Output is correct |
24 |
Correct |
4 ms |
3312 KB |
Output is correct |
25 |
Correct |
4 ms |
3332 KB |
Output is correct |
26 |
Correct |
8 ms |
5076 KB |
Output is correct |
27 |
Correct |
7 ms |
4948 KB |
Output is correct |
28 |
Correct |
5 ms |
3412 KB |
Output is correct |
29 |
Correct |
2 ms |
3284 KB |
Output is correct |
30 |
Correct |
2 ms |
3156 KB |
Output is correct |
31 |
Correct |
2 ms |
3156 KB |
Output is correct |
32 |
Correct |
2 ms |
3156 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Correct |
4 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
8 |
Correct |
2 ms |
3040 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
4 ms |
3668 KB |
Output is correct |
12 |
Correct |
3 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
4 ms |
3540 KB |
Output is correct |
15 |
Correct |
4 ms |
3540 KB |
Output is correct |
16 |
Correct |
3 ms |
3284 KB |
Output is correct |
17 |
Correct |
5 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3156 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
2 ms |
3156 KB |
Output is correct |
22 |
Correct |
3 ms |
3156 KB |
Output is correct |
23 |
Correct |
3 ms |
3156 KB |
Output is correct |
24 |
Correct |
5 ms |
3412 KB |
Output is correct |
25 |
Correct |
4 ms |
3392 KB |
Output is correct |
26 |
Correct |
7 ms |
4972 KB |
Output is correct |
27 |
Correct |
6 ms |
4948 KB |
Output is correct |
28 |
Correct |
5 ms |
3412 KB |
Output is correct |
29 |
Correct |
3 ms |
3284 KB |
Output is correct |
30 |
Correct |
2 ms |
3156 KB |
Output is correct |
31 |
Correct |
2 ms |
3156 KB |
Output is correct |
32 |
Correct |
2 ms |
3156 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Correct |
4 ms |
3540 KB |
Output is correct |
35 |
Correct |
53 ms |
14084 KB |
Output is correct |
36 |
Correct |
7 ms |
3924 KB |
Output is correct |
37 |
Correct |
66 ms |
14572 KB |
Output is correct |
38 |
Correct |
78 ms |
17744 KB |
Output is correct |
39 |
Correct |
78 ms |
18028 KB |
Output is correct |
40 |
Correct |
75 ms |
17944 KB |
Output is correct |
41 |
Correct |
76 ms |
17924 KB |
Output is correct |
42 |
Correct |
96 ms |
34296 KB |
Output is correct |
43 |
Correct |
80 ms |
34648 KB |
Output is correct |
44 |
Correct |
12 ms |
4964 KB |
Output is correct |
45 |
Correct |
61 ms |
19176 KB |
Output is correct |
46 |
Correct |
61 ms |
19408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3124 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
8 |
Correct |
2 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
4 ms |
3668 KB |
Output is correct |
12 |
Correct |
3 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
4 ms |
3624 KB |
Output is correct |
15 |
Correct |
4 ms |
3540 KB |
Output is correct |
16 |
Correct |
3 ms |
3284 KB |
Output is correct |
17 |
Correct |
5 ms |
3496 KB |
Output is correct |
18 |
Correct |
3 ms |
3156 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
3 ms |
3156 KB |
Output is correct |
22 |
Correct |
2 ms |
3156 KB |
Output is correct |
23 |
Correct |
3 ms |
3156 KB |
Output is correct |
24 |
Correct |
5 ms |
3412 KB |
Output is correct |
25 |
Correct |
4 ms |
3292 KB |
Output is correct |
26 |
Correct |
8 ms |
4948 KB |
Output is correct |
27 |
Correct |
7 ms |
4948 KB |
Output is correct |
28 |
Correct |
5 ms |
3412 KB |
Output is correct |
29 |
Correct |
2 ms |
3284 KB |
Output is correct |
30 |
Correct |
2 ms |
3156 KB |
Output is correct |
31 |
Correct |
2 ms |
3156 KB |
Output is correct |
32 |
Correct |
2 ms |
3156 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Correct |
4 ms |
3540 KB |
Output is correct |
35 |
Correct |
56 ms |
14052 KB |
Output is correct |
36 |
Correct |
8 ms |
3924 KB |
Output is correct |
37 |
Correct |
67 ms |
14556 KB |
Output is correct |
38 |
Correct |
78 ms |
17776 KB |
Output is correct |
39 |
Correct |
77 ms |
17996 KB |
Output is correct |
40 |
Correct |
77 ms |
17940 KB |
Output is correct |
41 |
Correct |
75 ms |
17836 KB |
Output is correct |
42 |
Correct |
98 ms |
34292 KB |
Output is correct |
43 |
Correct |
86 ms |
34688 KB |
Output is correct |
44 |
Correct |
12 ms |
4948 KB |
Output is correct |
45 |
Correct |
58 ms |
19248 KB |
Output is correct |
46 |
Correct |
58 ms |
19268 KB |
Output is correct |
47 |
Correct |
135 ms |
14280 KB |
Output is correct |
48 |
Correct |
38 ms |
5868 KB |
Output is correct |
49 |
Correct |
29 ms |
5324 KB |
Output is correct |
50 |
Correct |
24 ms |
4716 KB |
Output is correct |
51 |
Correct |
71 ms |
7660 KB |
Output is correct |
52 |
Correct |
73 ms |
7980 KB |
Output is correct |
53 |
Correct |
35 ms |
6588 KB |
Output is correct |
54 |
Correct |
2 ms |
3156 KB |
Output is correct |
55 |
Correct |
2 ms |
3156 KB |
Output is correct |
56 |
Correct |
30 ms |
6572 KB |
Output is correct |
57 |
Correct |
37 ms |
3788 KB |
Output is correct |
58 |
Correct |
17 ms |
5904 KB |
Output is correct |
59 |
Correct |
42 ms |
12008 KB |
Output is correct |
60 |
Correct |
269 ms |
64352 KB |
Output is correct |
61 |
Correct |
758 ms |
219932 KB |
Output is correct |
62 |
Correct |
71 ms |
8536 KB |
Output is correct |
63 |
Correct |
33 ms |
8268 KB |
Output is correct |
64 |
Correct |
35 ms |
8108 KB |
Output is correct |
65 |
Correct |
37 ms |
10244 KB |
Output is correct |
66 |
Correct |
60 ms |
19452 KB |
Output is correct |
67 |
Correct |
60 ms |
19396 KB |
Output is correct |