#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define mp make_pair
#define ll long long
#define migmig cin.tie(NULL);ios_base::sync_with_stdio(false)
#define max_heap(T) priority_queue<T>
#define min_heap(T) priority_queue<T, vector<T>, greater<T>>
#define pb push_back
#define fi first
#define sec second
#define endl "\n"
#define pii pair <int, int>
using namespace std;
const ll MOD = 1e9 + 7;
const int INF = 1e9;
const ll INF18 = 1e18;
const int MAXN = 3e4 + 1;
const int LOG = 23;
vector <int> all[MAXN];
map <pii, int> shit;
set <pii> s;
bool be[MAXN];
vector <pii> adj[MAXN];
ll dist[MAXN];
int now = 0;
int t0, t1;
int ft = 0;
bool flag = 0;
void dijk(int r){
min_heap(pii) pq;
dist[r] = 0;
pq.push(mp(0, r));
while (!pq.empty()){
ft++;
auto [d, v] = pq.top();
if (v == t1) return;
pq.pop();
if (d == dist[v]){
for (auto [u, w] : adj[v]){
if (dist[u] > d + w){
dist[u] = d + w;
pq.push(mp(d + w, u));
}
}
}
}
}
void solve(){
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++){
int b, p;
cin >> b >> p;
if (i == 0) t0 = b;
if (i == 1) t1 = b;
if (s.find(mp(b % p, p)) == s.end()) shit[mp(b % p, p)] = now++, s.insert(mp(b % p, p));
be[b] = 1;
all[shit[mp(b % p, p)]].pb(b);
}
vector <int> v;
for (auto i : s){
v.clear();
int t = shit[i];
for (int j : all[t]) v.pb(j);
sort(v.begin(), v.end());
int last = i.fi;
for (int j = 0; j < v.size(); j++){
for (int k = last; k < v[j]; k += i.sec){
if (be[k]) adj[v[j]].pb(mp(k, (v[j] - k) / i.sec));
}
for (int k = v[j] + i.sec; k < n; k += i.sec){
if (be[k]) adj[v[j]].pb(mp(k, (k - v[j]) / i.sec));
if (j != v.size() - 1 && k == v[j + 1]) break;
}
last = v[j];
}
}
fill(dist, dist + n, INF);
dijk(t0);
if (dist[t1] == INF) cout << -1 << endl;
else cout << dist[t1] << endl;
}
int main(){
migmig;
int tc = 1;
// cin >> tc;
for (int ti = 0; ti < tc; ti++) solve();
}
Compilation message
skyscraper.cpp: In function 'void dijk(int)':
skyscraper.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | auto [d, v] = pq.top();
| ^
skyscraper.cpp:40:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
40 | for (auto [u, w] : adj[v]){
| ^
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:68:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int j = 0; j < v.size(); j++){
| ~~^~~~~~~~~~
skyscraper.cpp:74:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if (j != v.size() - 1 && k == v[j + 1]) break;
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1884 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
3 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1884 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
1888 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2040 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
2044 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
2096 KB |
Output is correct |
10 |
Correct |
2 ms |
2044 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2132 KB |
Output is correct |
16 |
Correct |
3 ms |
1876 KB |
Output is correct |
17 |
Correct |
4 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
1888 KB |
Output is correct |
19 |
Correct |
2 ms |
1888 KB |
Output is correct |
20 |
Correct |
3 ms |
2040 KB |
Output is correct |
21 |
Correct |
4 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
3 ms |
1876 KB |
Output is correct |
24 |
Correct |
4 ms |
2532 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
26 |
Correct |
3 ms |
2132 KB |
Output is correct |
27 |
Correct |
4 ms |
2256 KB |
Output is correct |
28 |
Correct |
4 ms |
2388 KB |
Output is correct |
29 |
Correct |
2 ms |
1876 KB |
Output is correct |
30 |
Correct |
2 ms |
1880 KB |
Output is correct |
31 |
Correct |
2 ms |
1876 KB |
Output is correct |
32 |
Correct |
2 ms |
1876 KB |
Output is correct |
33 |
Correct |
3 ms |
2132 KB |
Output is correct |
34 |
Correct |
3 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2132 KB |
Output is correct |
13 |
Correct |
2 ms |
2040 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2140 KB |
Output is correct |
16 |
Correct |
3 ms |
1876 KB |
Output is correct |
17 |
Correct |
4 ms |
2296 KB |
Output is correct |
18 |
Correct |
2 ms |
1876 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
2 ms |
1876 KB |
Output is correct |
21 |
Correct |
3 ms |
2040 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1888 KB |
Output is correct |
24 |
Correct |
4 ms |
2132 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
26 |
Correct |
3 ms |
2132 KB |
Output is correct |
27 |
Correct |
4 ms |
2512 KB |
Output is correct |
28 |
Correct |
4 ms |
2404 KB |
Output is correct |
29 |
Correct |
2 ms |
1876 KB |
Output is correct |
30 |
Correct |
2 ms |
1876 KB |
Output is correct |
31 |
Correct |
2 ms |
1884 KB |
Output is correct |
32 |
Correct |
3 ms |
2044 KB |
Output is correct |
33 |
Correct |
3 ms |
2132 KB |
Output is correct |
34 |
Correct |
5 ms |
2132 KB |
Output is correct |
35 |
Correct |
28 ms |
6484 KB |
Output is correct |
36 |
Correct |
6 ms |
2644 KB |
Output is correct |
37 |
Correct |
23 ms |
6772 KB |
Output is correct |
38 |
Correct |
45 ms |
8072 KB |
Output is correct |
39 |
Correct |
34 ms |
7900 KB |
Output is correct |
40 |
Correct |
44 ms |
8020 KB |
Output is correct |
41 |
Correct |
34 ms |
8012 KB |
Output is correct |
42 |
Correct |
13 ms |
8388 KB |
Output is correct |
43 |
Correct |
23 ms |
8648 KB |
Output is correct |
44 |
Correct |
6 ms |
2644 KB |
Output is correct |
45 |
Correct |
35 ms |
6476 KB |
Output is correct |
46 |
Correct |
30 ms |
6460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
4 ms |
2132 KB |
Output is correct |
18 |
Correct |
3 ms |
1876 KB |
Output is correct |
19 |
Correct |
3 ms |
1876 KB |
Output is correct |
20 |
Correct |
2 ms |
2040 KB |
Output is correct |
21 |
Correct |
3 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1876 KB |
Output is correct |
24 |
Correct |
5 ms |
2132 KB |
Output is correct |
25 |
Correct |
4 ms |
2300 KB |
Output is correct |
26 |
Correct |
2 ms |
2132 KB |
Output is correct |
27 |
Correct |
3 ms |
2256 KB |
Output is correct |
28 |
Correct |
3 ms |
2388 KB |
Output is correct |
29 |
Correct |
2 ms |
1876 KB |
Output is correct |
30 |
Correct |
2 ms |
1876 KB |
Output is correct |
31 |
Correct |
2 ms |
1876 KB |
Output is correct |
32 |
Correct |
2 ms |
2040 KB |
Output is correct |
33 |
Correct |
3 ms |
2156 KB |
Output is correct |
34 |
Correct |
3 ms |
2132 KB |
Output is correct |
35 |
Correct |
28 ms |
6708 KB |
Output is correct |
36 |
Correct |
7 ms |
2900 KB |
Output is correct |
37 |
Correct |
25 ms |
6740 KB |
Output is correct |
38 |
Correct |
37 ms |
8012 KB |
Output is correct |
39 |
Correct |
34 ms |
8020 KB |
Output is correct |
40 |
Correct |
33 ms |
8020 KB |
Output is correct |
41 |
Correct |
33 ms |
8072 KB |
Output is correct |
42 |
Correct |
13 ms |
8388 KB |
Output is correct |
43 |
Correct |
22 ms |
8644 KB |
Output is correct |
44 |
Correct |
5 ms |
2656 KB |
Output is correct |
45 |
Correct |
32 ms |
6540 KB |
Output is correct |
46 |
Correct |
29 ms |
6648 KB |
Output is correct |
47 |
Correct |
39 ms |
13276 KB |
Output is correct |
48 |
Correct |
28 ms |
6900 KB |
Output is correct |
49 |
Correct |
28 ms |
6076 KB |
Output is correct |
50 |
Correct |
18 ms |
4948 KB |
Output is correct |
51 |
Correct |
40 ms |
9432 KB |
Output is correct |
52 |
Correct |
44 ms |
9672 KB |
Output is correct |
53 |
Correct |
32 ms |
7248 KB |
Output is correct |
54 |
Correct |
3 ms |
1876 KB |
Output is correct |
55 |
Correct |
2 ms |
1876 KB |
Output is correct |
56 |
Correct |
10 ms |
3412 KB |
Output is correct |
57 |
Correct |
5 ms |
2756 KB |
Output is correct |
58 |
Correct |
938 ms |
2892 KB |
Output is correct |
59 |
Correct |
25 ms |
8404 KB |
Output is correct |
60 |
Correct |
806 ms |
70044 KB |
Output is correct |
61 |
Correct |
178 ms |
37796 KB |
Output is correct |
62 |
Correct |
42 ms |
11468 KB |
Output is correct |
63 |
Correct |
34 ms |
6740 KB |
Output is correct |
64 |
Correct |
31 ms |
6484 KB |
Output is correct |
65 |
Correct |
34 ms |
6740 KB |
Output is correct |
66 |
Correct |
35 ms |
6820 KB |
Output is correct |
67 |
Correct |
44 ms |
6744 KB |
Output is correct |