/*************************************
* author: marvinthang *
* created: 03.02.2023 14:54:32 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// end of template
const int MAX = 5e4 + 4;
const int BLOCK_SIZE = 150;
int N, M, B[MAX], P[MAX];
void init(void) {
cin >> N >> M;
REP(i, M) cin >> B[i] >> P[i];
}
bool exist[BLOCK_SIZE][BLOCK_SIZE];
vector <pair <int, int>> adj[MAX];
pair <int, int> rev_id[MAX * BLOCK_SIZE];
int dist[MAX * BLOCK_SIZE + MAX];
int id[BLOCK_SIZE][MAX];
bool inQueue[MAX * BLOCK_SIZE + MAX];
void process(void) {
REP(i, M) if (P[i] < BLOCK_SIZE) exist[P[i]][B[i] % P[i]] = true;
int nn = N;
FOR(i, 1, BLOCK_SIZE - 1) REP(j, i) if (exist[i][j]) for (int k = j; k < N; k += i) {
id[i][k] = nn++;
rev_id[id[i][k] - N] = make_pair(i, k);
}
REP(i, M) {
if (P[i] >= BLOCK_SIZE) {
for (int j = 1, k = B[i]; (k -= P[i]) >= 0; ++j)
adj[B[i]].push_back(make_pair(j, k));
for (int j = 1; B[i] + j * P[i] < N; ++j)
adj[B[i]].push_back(make_pair(j, B[i] + j * P[i]));
} else adj[B[i]].push_back(make_pair(0, id[P[i]][B[i]]));
}
queue <int> q;
memset(dist, 0x3f, sizeof(dist));
dist[B[0]] = 0;
q.push(B[0]);
inQueue[B[0]] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
inQueue[u] = false;
if (u >= N) {
auto &[p, b] = rev_id[u - N];
if (minimize(dist[b], dist[u]) && !inQueue[b]) {
q.push(b);
inQueue[b] = true;
}
if (b >= p && minimize(dist[id[p][b - p]], dist[u] + 1) && !inQueue[id[p][b - p]]) {
q.push(id[p][b - p]);
inQueue[id[p][b - p]] = true;
}
if (b + p < N && minimize(dist[id[p][b + p]], dist[u] + 1) && !inQueue[id[p][b + p]]) {
q.push(id[p][b + p]);
inQueue[id[p][b + p]] = true;
}
} else {
for (auto &[w, v]: adj[u]) if (minimize(dist[v], dist[u] + w) && !inQueue[v]) {
q.push(v);
inQueue[v] = true;
}
}
}
cout << (dist[B[1]] < 1e9 ? dist[B[1]] : -1) << '\n';
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("skyscraper");
init();
process();
// cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:111:2: note: in expansion of macro 'file'
111 | file("skyscraper");
| ^~~~
skyscraper.cpp:27:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:111:2: note: in expansion of macro 'file'
111 | file("skyscraper");
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
38232 KB |
Output is correct |
2 |
Correct |
5 ms |
38236 KB |
Output is correct |
3 |
Correct |
5 ms |
38200 KB |
Output is correct |
4 |
Correct |
5 ms |
38424 KB |
Output is correct |
5 |
Correct |
5 ms |
40284 KB |
Output is correct |
6 |
Correct |
5 ms |
38232 KB |
Output is correct |
7 |
Correct |
5 ms |
38232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
38236 KB |
Output is correct |
2 |
Correct |
5 ms |
38236 KB |
Output is correct |
3 |
Correct |
5 ms |
38236 KB |
Output is correct |
4 |
Correct |
5 ms |
38172 KB |
Output is correct |
5 |
Correct |
5 ms |
40284 KB |
Output is correct |
6 |
Correct |
5 ms |
38168 KB |
Output is correct |
7 |
Correct |
5 ms |
38236 KB |
Output is correct |
8 |
Correct |
7 ms |
56668 KB |
Output is correct |
9 |
Correct |
6 ms |
50460 KB |
Output is correct |
10 |
Correct |
7 ms |
58656 KB |
Output is correct |
11 |
Correct |
7 ms |
58716 KB |
Output is correct |
12 |
Correct |
7 ms |
56668 KB |
Output is correct |
13 |
Correct |
5 ms |
38236 KB |
Output is correct |
14 |
Correct |
7 ms |
50524 KB |
Output is correct |
15 |
Correct |
6 ms |
50524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
38236 KB |
Output is correct |
2 |
Correct |
5 ms |
38236 KB |
Output is correct |
3 |
Correct |
5 ms |
38236 KB |
Output is correct |
4 |
Correct |
5 ms |
38236 KB |
Output is correct |
5 |
Correct |
6 ms |
40484 KB |
Output is correct |
6 |
Correct |
5 ms |
38236 KB |
Output is correct |
7 |
Correct |
5 ms |
38236 KB |
Output is correct |
8 |
Correct |
7 ms |
56652 KB |
Output is correct |
9 |
Correct |
6 ms |
50776 KB |
Output is correct |
10 |
Correct |
7 ms |
58712 KB |
Output is correct |
11 |
Correct |
7 ms |
58716 KB |
Output is correct |
12 |
Correct |
7 ms |
56672 KB |
Output is correct |
13 |
Correct |
5 ms |
38236 KB |
Output is correct |
14 |
Correct |
7 ms |
50524 KB |
Output is correct |
15 |
Correct |
7 ms |
50524 KB |
Output is correct |
16 |
Correct |
7 ms |
64828 KB |
Output is correct |
17 |
Correct |
8 ms |
64860 KB |
Output is correct |
18 |
Correct |
9 ms |
65116 KB |
Output is correct |
19 |
Correct |
7 ms |
64860 KB |
Output is correct |
20 |
Correct |
5 ms |
38300 KB |
Output is correct |
21 |
Correct |
7 ms |
64860 KB |
Output is correct |
22 |
Correct |
8 ms |
64860 KB |
Output is correct |
23 |
Correct |
8 ms |
64860 KB |
Output is correct |
24 |
Correct |
9 ms |
64860 KB |
Output is correct |
25 |
Correct |
6 ms |
38236 KB |
Output is correct |
26 |
Correct |
8 ms |
64860 KB |
Output is correct |
27 |
Correct |
9 ms |
64856 KB |
Output is correct |
28 |
Correct |
8 ms |
64832 KB |
Output is correct |
29 |
Correct |
8 ms |
46428 KB |
Output is correct |
30 |
Correct |
6 ms |
42328 KB |
Output is correct |
31 |
Correct |
7 ms |
42660 KB |
Output is correct |
32 |
Correct |
6 ms |
42332 KB |
Output is correct |
33 |
Correct |
8 ms |
50724 KB |
Output is correct |
34 |
Correct |
9 ms |
50740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
38236 KB |
Output is correct |
2 |
Correct |
5 ms |
38236 KB |
Output is correct |
3 |
Correct |
5 ms |
38236 KB |
Output is correct |
4 |
Correct |
5 ms |
38236 KB |
Output is correct |
5 |
Correct |
5 ms |
40280 KB |
Output is correct |
6 |
Correct |
5 ms |
38232 KB |
Output is correct |
7 |
Correct |
5 ms |
38244 KB |
Output is correct |
8 |
Correct |
7 ms |
56608 KB |
Output is correct |
9 |
Correct |
6 ms |
50520 KB |
Output is correct |
10 |
Correct |
8 ms |
58944 KB |
Output is correct |
11 |
Correct |
7 ms |
58712 KB |
Output is correct |
12 |
Correct |
8 ms |
56668 KB |
Output is correct |
13 |
Correct |
5 ms |
38236 KB |
Output is correct |
14 |
Correct |
6 ms |
50468 KB |
Output is correct |
15 |
Correct |
7 ms |
50536 KB |
Output is correct |
16 |
Correct |
7 ms |
64860 KB |
Output is correct |
17 |
Correct |
9 ms |
64860 KB |
Output is correct |
18 |
Correct |
8 ms |
64860 KB |
Output is correct |
19 |
Correct |
8 ms |
64860 KB |
Output is correct |
20 |
Correct |
6 ms |
38236 KB |
Output is correct |
21 |
Correct |
8 ms |
64860 KB |
Output is correct |
22 |
Correct |
7 ms |
64860 KB |
Output is correct |
23 |
Correct |
8 ms |
64860 KB |
Output is correct |
24 |
Correct |
9 ms |
64860 KB |
Output is correct |
25 |
Correct |
5 ms |
38276 KB |
Output is correct |
26 |
Correct |
8 ms |
64856 KB |
Output is correct |
27 |
Correct |
8 ms |
64860 KB |
Output is correct |
28 |
Correct |
9 ms |
64860 KB |
Output is correct |
29 |
Correct |
7 ms |
46364 KB |
Output is correct |
30 |
Correct |
6 ms |
42332 KB |
Output is correct |
31 |
Correct |
7 ms |
42332 KB |
Output is correct |
32 |
Correct |
6 ms |
42328 KB |
Output is correct |
33 |
Correct |
10 ms |
50908 KB |
Output is correct |
34 |
Correct |
11 ms |
50524 KB |
Output is correct |
35 |
Correct |
14 ms |
65792 KB |
Output is correct |
36 |
Correct |
9 ms |
64860 KB |
Output is correct |
37 |
Correct |
15 ms |
66136 KB |
Output is correct |
38 |
Correct |
16 ms |
66140 KB |
Output is correct |
39 |
Correct |
15 ms |
66140 KB |
Output is correct |
40 |
Correct |
20 ms |
66132 KB |
Output is correct |
41 |
Correct |
15 ms |
66136 KB |
Output is correct |
42 |
Correct |
11 ms |
65696 KB |
Output is correct |
43 |
Correct |
12 ms |
65584 KB |
Output is correct |
44 |
Correct |
8 ms |
38936 KB |
Output is correct |
45 |
Correct |
22 ms |
69940 KB |
Output is correct |
46 |
Correct |
19 ms |
69712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
38236 KB |
Output is correct |
2 |
Correct |
5 ms |
38172 KB |
Output is correct |
3 |
Correct |
5 ms |
38488 KB |
Output is correct |
4 |
Correct |
5 ms |
38188 KB |
Output is correct |
5 |
Correct |
5 ms |
40284 KB |
Output is correct |
6 |
Correct |
5 ms |
38236 KB |
Output is correct |
7 |
Correct |
5 ms |
38236 KB |
Output is correct |
8 |
Correct |
7 ms |
56668 KB |
Output is correct |
9 |
Correct |
7 ms |
50524 KB |
Output is correct |
10 |
Correct |
7 ms |
58716 KB |
Output is correct |
11 |
Correct |
7 ms |
58728 KB |
Output is correct |
12 |
Correct |
7 ms |
56744 KB |
Output is correct |
13 |
Correct |
5 ms |
38236 KB |
Output is correct |
14 |
Correct |
6 ms |
50524 KB |
Output is correct |
15 |
Correct |
6 ms |
50604 KB |
Output is correct |
16 |
Correct |
8 ms |
64856 KB |
Output is correct |
17 |
Correct |
8 ms |
64804 KB |
Output is correct |
18 |
Correct |
8 ms |
64860 KB |
Output is correct |
19 |
Correct |
7 ms |
64972 KB |
Output is correct |
20 |
Correct |
5 ms |
38236 KB |
Output is correct |
21 |
Correct |
9 ms |
64860 KB |
Output is correct |
22 |
Correct |
8 ms |
64860 KB |
Output is correct |
23 |
Correct |
9 ms |
64956 KB |
Output is correct |
24 |
Correct |
8 ms |
65012 KB |
Output is correct |
25 |
Correct |
5 ms |
38236 KB |
Output is correct |
26 |
Correct |
8 ms |
64860 KB |
Output is correct |
27 |
Correct |
8 ms |
64856 KB |
Output is correct |
28 |
Correct |
8 ms |
65028 KB |
Output is correct |
29 |
Correct |
8 ms |
46428 KB |
Output is correct |
30 |
Correct |
6 ms |
42344 KB |
Output is correct |
31 |
Correct |
6 ms |
42332 KB |
Output is correct |
32 |
Correct |
6 ms |
42332 KB |
Output is correct |
33 |
Correct |
9 ms |
50596 KB |
Output is correct |
34 |
Correct |
9 ms |
50760 KB |
Output is correct |
35 |
Correct |
14 ms |
65624 KB |
Output is correct |
36 |
Correct |
10 ms |
64860 KB |
Output is correct |
37 |
Correct |
17 ms |
66140 KB |
Output is correct |
38 |
Correct |
16 ms |
66136 KB |
Output is correct |
39 |
Correct |
19 ms |
66140 KB |
Output is correct |
40 |
Correct |
18 ms |
66144 KB |
Output is correct |
41 |
Correct |
16 ms |
66140 KB |
Output is correct |
42 |
Correct |
12 ms |
65756 KB |
Output is correct |
43 |
Correct |
11 ms |
65756 KB |
Output is correct |
44 |
Correct |
9 ms |
38916 KB |
Output is correct |
45 |
Correct |
19 ms |
69904 KB |
Output is correct |
46 |
Correct |
21 ms |
70048 KB |
Output is correct |
47 |
Correct |
37 ms |
71516 KB |
Output is correct |
48 |
Correct |
13 ms |
66396 KB |
Output is correct |
49 |
Correct |
13 ms |
66264 KB |
Output is correct |
50 |
Correct |
12 ms |
65832 KB |
Output is correct |
51 |
Correct |
28 ms |
67416 KB |
Output is correct |
52 |
Correct |
30 ms |
67416 KB |
Output is correct |
53 |
Correct |
10 ms |
39000 KB |
Output is correct |
54 |
Correct |
5 ms |
38424 KB |
Output is correct |
55 |
Correct |
5 ms |
38236 KB |
Output is correct |
56 |
Correct |
11 ms |
39508 KB |
Output is correct |
57 |
Correct |
21 ms |
53084 KB |
Output is correct |
58 |
Correct |
11 ms |
51372 KB |
Output is correct |
59 |
Correct |
12 ms |
51420 KB |
Output is correct |
60 |
Correct |
15 ms |
66004 KB |
Output is correct |
61 |
Correct |
13 ms |
65948 KB |
Output is correct |
62 |
Correct |
25 ms |
70068 KB |
Output is correct |
63 |
Correct |
38 ms |
62044 KB |
Output is correct |
64 |
Correct |
46 ms |
70464 KB |
Output is correct |
65 |
Correct |
72 ms |
84828 KB |
Output is correct |
66 |
Correct |
146 ms |
129108 KB |
Output is correct |
67 |
Correct |
150 ms |
128844 KB |
Output is correct |