#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Cay ngay cay dem nhung deo duoc cong nhan
*/
const int oo = 1e9;
const int N = 30001;
int n, m, b[N], p[N], rs = oo;
vi a[N];
map<pi, int> d;
deque<pi> q;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> m;
FOR(i, 0, m - 1) {
cin >> b[i] >> p[i];
a[b[i]].pb(p[i]);
}
FOR(i, 0, n - 1) sort(all(a[i]));
d[{b[0], p[0]}] = 0;
q.pb({b[0], p[0]});
WHILE(!q.empty()) {
pi cur = q.front();
q._pf();
int b = cur.st, p = cur.nd;
int du = d[{b, p}];
if (b - p >= 0 && !d.count({b - p, p})) {
d[{b - p, p}] = du + 1;
q.pb({b - p, p});
}
if (b + p < n && !d.count({b + p, p})) {
d[{b + p, p}] = du + 1;
q.pb({b + p, p});
}
if (!a[b].empty()) {
if (a[b].back() < p) {
int p1 = a[b][0];
if (!d.count({b, p1}) || d[{b, p1}] > du) {
d[{b, p1}] = du;
q.pf({b, p1});
}
}
else {
int pos = lb(all(a[b]), p) - a[b].begin();
if (a[b][pos] != p) {
int p1 = a[b][0];
if (!d.count({b, p1}) || d[{b, p1}] > du) {
d[{b, p1}] = du;
q.pf({b, p1});
}
}
else {
if (pos > 0) {
int p1 = a[b][pos - 1];
if (!d.count({b, p1}) || d[{b, p1}] > du) {
d[{b, p1}] = du;
q.pf({b, p1});
}
}
if (pos < sz(a[b]) - 1) {
int p1 = a[b][pos + 1];
if (!d.count({b, p1}) || d[{b, p1}] > du) {
d[{b, p1}] = du;
q.pf({b, p1});
}
}
}
}
}
}
FOR(i, 1, N - 1) {
if (!d.count({b[1], i})) continue;
rs = min(rs, d[{b[1], i}]);
}
cout << (rs < oo? rs : -1);
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
skyscraper.cpp:47:5: note: in expansion of macro 'FOR'
47 | FOR(i, 0, m - 1) {
| ^~~
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
skyscraper.cpp:51:5: note: in expansion of macro 'FOR'
51 | FOR(i, 0, n - 1) sort(all(a[i]));
| ^~~
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
skyscraper.cpp:103:5: note: in expansion of macro 'FOR'
103 | FOR(i, 1, N - 1) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1044 KB |
Output is correct |
2 |
Correct |
1 ms |
1048 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1040 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1036 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
1048 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
4 ms |
1236 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
6 ms |
1364 KB |
Output is correct |
15 |
Correct |
5 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1044 KB |
Output is correct |
2 |
Correct |
1 ms |
1044 KB |
Output is correct |
3 |
Correct |
1 ms |
1040 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1040 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
4 ms |
1236 KB |
Output is correct |
12 |
Correct |
2 ms |
1048 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
5 ms |
1436 KB |
Output is correct |
15 |
Correct |
5 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
13 ms |
2008 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
3 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
6 ms |
1364 KB |
Output is correct |
24 |
Correct |
10 ms |
1864 KB |
Output is correct |
25 |
Correct |
6 ms |
1492 KB |
Output is correct |
26 |
Correct |
5 ms |
1440 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
16 ms |
2460 KB |
Output is correct |
29 |
Correct |
44 ms |
4812 KB |
Output is correct |
30 |
Correct |
11 ms |
2032 KB |
Output is correct |
31 |
Correct |
22 ms |
3056 KB |
Output is correct |
32 |
Correct |
15 ms |
2424 KB |
Output is correct |
33 |
Correct |
86 ms |
8580 KB |
Output is correct |
34 |
Correct |
86 ms |
8588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1040 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1032 KB |
Output is correct |
6 |
Correct |
1 ms |
1036 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
1060 KB |
Output is correct |
10 |
Correct |
3 ms |
1108 KB |
Output is correct |
11 |
Correct |
5 ms |
1300 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
6 ms |
1364 KB |
Output is correct |
15 |
Correct |
4 ms |
1428 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
13 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
3 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
6 ms |
1364 KB |
Output is correct |
24 |
Correct |
11 ms |
1792 KB |
Output is correct |
25 |
Correct |
6 ms |
1492 KB |
Output is correct |
26 |
Correct |
4 ms |
1492 KB |
Output is correct |
27 |
Correct |
4 ms |
1436 KB |
Output is correct |
28 |
Correct |
17 ms |
2496 KB |
Output is correct |
29 |
Correct |
39 ms |
4868 KB |
Output is correct |
30 |
Correct |
10 ms |
2060 KB |
Output is correct |
31 |
Correct |
19 ms |
3028 KB |
Output is correct |
32 |
Correct |
15 ms |
2452 KB |
Output is correct |
33 |
Correct |
84 ms |
8524 KB |
Output is correct |
34 |
Correct |
87 ms |
8552 KB |
Output is correct |
35 |
Correct |
118 ms |
7876 KB |
Output is correct |
36 |
Correct |
17 ms |
2200 KB |
Output is correct |
37 |
Correct |
247 ms |
12184 KB |
Output is correct |
38 |
Correct |
211 ms |
11612 KB |
Output is correct |
39 |
Correct |
212 ms |
11664 KB |
Output is correct |
40 |
Correct |
214 ms |
11668 KB |
Output is correct |
41 |
Correct |
213 ms |
11596 KB |
Output is correct |
42 |
Correct |
8 ms |
2020 KB |
Output is correct |
43 |
Correct |
8 ms |
1876 KB |
Output is correct |
44 |
Correct |
6 ms |
1692 KB |
Output is correct |
45 |
Correct |
634 ms |
31764 KB |
Output is correct |
46 |
Correct |
592 ms |
31820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1040 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
5 ms |
1300 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
1044 KB |
Output is correct |
14 |
Correct |
5 ms |
1364 KB |
Output is correct |
15 |
Correct |
4 ms |
1432 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
14 ms |
1988 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
2 ms |
1044 KB |
Output is correct |
20 |
Correct |
2 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
5 ms |
1428 KB |
Output is correct |
24 |
Correct |
10 ms |
1820 KB |
Output is correct |
25 |
Correct |
6 ms |
1564 KB |
Output is correct |
26 |
Correct |
5 ms |
1436 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
17 ms |
2460 KB |
Output is correct |
29 |
Correct |
38 ms |
4864 KB |
Output is correct |
30 |
Correct |
9 ms |
2132 KB |
Output is correct |
31 |
Correct |
19 ms |
3028 KB |
Output is correct |
32 |
Correct |
14 ms |
2440 KB |
Output is correct |
33 |
Correct |
93 ms |
8540 KB |
Output is correct |
34 |
Correct |
90 ms |
8516 KB |
Output is correct |
35 |
Correct |
115 ms |
7860 KB |
Output is correct |
36 |
Correct |
17 ms |
2220 KB |
Output is correct |
37 |
Correct |
243 ms |
12096 KB |
Output is correct |
38 |
Correct |
218 ms |
11680 KB |
Output is correct |
39 |
Correct |
206 ms |
11680 KB |
Output is correct |
40 |
Correct |
216 ms |
11660 KB |
Output is correct |
41 |
Correct |
216 ms |
11676 KB |
Output is correct |
42 |
Correct |
9 ms |
2004 KB |
Output is correct |
43 |
Correct |
10 ms |
1944 KB |
Output is correct |
44 |
Correct |
6 ms |
1748 KB |
Output is correct |
45 |
Correct |
602 ms |
31728 KB |
Output is correct |
46 |
Correct |
597 ms |
31832 KB |
Output is correct |
47 |
Execution timed out |
1084 ms |
32828 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |