#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
using ld = long double;
using db = double;
///--------------------------------
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mem(f, x) memset(f, x, sizeof(f))
#define uniqueV(x) sort(all(x)), (x).resize(unique(all(x)) - x.begin())
template<class T> bool maximize(T &a, const T &b){ return (a < b ? a = b, 1 : 0); }
template<class T> bool minimize(T &a, const T &b){ return (a > b ? a = b, 1 : 0); }
///--------------------------------
#define PROBLEM "test"
const int MOD = 1e9 + 7; // 998244353;
const ll INF = 1e18;
const ld eps = 1e-9;
const ld PI = acos(-1);
const int dx[4]{0, 1, 0, -1}, dy[4]{1, 0, -1, 0}; // R D L U
const int ddx[4]{-1, 1, 1, -1}, ddy[4]{1, 1, -1, -1}; // UR DR DL UL
///--------------------------------
void precalc();
void solve();
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(PROBLEM".inp", "r")) {
freopen(PROBLEM".inp", "r", stdin);
freopen(PROBLEM".out", "w", stdout);
}
constexpr bool MULTI_TEST = 0;
int t = 1;
if (MULTI_TEST) cin >> t;
while (t--)
solve();
// cerr << setprecision(3) << fixed;
// cerr << "[" << 1.0 * clock() / CLOCKS_PER_SEC << "s] ";
}
///--------------------[PROBLEM SOLUTION]--------------------///
const int BLOCKSZ = 200;
const int maxn = 30030;
ll dist[maxn][BLOCKSZ + 1];
bool vis[maxn][BLOCKSZ + 1];
vector<ll> spec[maxn];
ll n, m, P[maxn], B[maxn];
priority_queue<array<ll, 3>, vector<array<ll, 3>>, greater<array<ll, 3>>> pq;
void addHeavy(int x, int y, ll D) {
for (int nx = x % y; nx < n; nx += y) {
if (!vis[nx][BLOCKSZ] && minimize(dist[nx][BLOCKSZ], D + abs(nx - x) / y))
pq.push({dist[nx][BLOCKSZ], nx, BLOCKSZ});
}
}
void solve() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> B[i] >> P[i];
spec[B[i]].pb(P[i]);
}
mem(dist, 0x3f);
mem(vis, false);
if (P[0] >= BLOCKSZ)
addHeavy(B[0], P[0], 0);
dist[B[0]][BLOCKSZ] = 0;
pq.push({0, B[0], BLOCKSZ});
while (pq.size()) {
int x = pq.top()[1], y = pq.top()[2]; pq.pop();
if (vis[x][y]) continue;
vis[x][y] = true;
if (y == BLOCKSZ) {
for (ll cy : spec[x]) {
if (cy < BLOCKSZ) {
if (!vis[x][cy] && minimize(dist[x][cy], dist[x][y]))
pq.push({dist[x][cy], x, cy});
}
else addHeavy(x, cy, dist[x][BLOCKSZ]);
}
spec[x].clear();
continue;
}
//dummy vertex
if (x - y >= 0 && !vis[x - y][BLOCKSZ]
&& minimize(dist[x - y][BLOCKSZ], dist[x][y] + 1))
pq.push({dist[x - y][BLOCKSZ], x - y, BLOCKSZ});
if (x + y < n && !vis[x + y][BLOCKSZ]
&& minimize(dist[x + y][BLOCKSZ], dist[x][y] + 1))
pq.push({dist[x + y][BLOCKSZ], x + y, BLOCKSZ});
//direct path
if (x - y >= 0 && !vis[x - y][y]
&& minimize(dist[x - y][y], dist[x][y] + 1))
pq.push({dist[x - y][y], x - y, y});
if (x + y < n && !vis[x + y][y]
&& minimize(dist[x + y][y], dist[x][y] + 1))
pq.push({dist[x + y][y], x + y, y});
}
cout << (!vis[B[1]][BLOCKSZ] ? -1 : dist[B[1]][BLOCKSZ]) << '\n';
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(PROBLEM".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen(PROBLEM".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
54620 KB |
Output is correct |
2 |
Correct |
7 ms |
54544 KB |
Output is correct |
3 |
Correct |
8 ms |
54364 KB |
Output is correct |
4 |
Correct |
8 ms |
54364 KB |
Output is correct |
5 |
Correct |
7 ms |
54516 KB |
Output is correct |
6 |
Correct |
7 ms |
54364 KB |
Output is correct |
7 |
Correct |
7 ms |
54364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
54612 KB |
Output is correct |
2 |
Correct |
7 ms |
54364 KB |
Output is correct |
3 |
Correct |
7 ms |
54364 KB |
Output is correct |
4 |
Correct |
7 ms |
54364 KB |
Output is correct |
5 |
Correct |
7 ms |
54364 KB |
Output is correct |
6 |
Correct |
7 ms |
54364 KB |
Output is correct |
7 |
Correct |
7 ms |
54372 KB |
Output is correct |
8 |
Correct |
7 ms |
54364 KB |
Output is correct |
9 |
Correct |
7 ms |
54620 KB |
Output is correct |
10 |
Correct |
7 ms |
54620 KB |
Output is correct |
11 |
Correct |
8 ms |
54616 KB |
Output is correct |
12 |
Correct |
7 ms |
54620 KB |
Output is correct |
13 |
Correct |
7 ms |
54608 KB |
Output is correct |
14 |
Correct |
8 ms |
54616 KB |
Output is correct |
15 |
Correct |
8 ms |
54620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
54364 KB |
Output is correct |
2 |
Correct |
7 ms |
54516 KB |
Output is correct |
3 |
Correct |
7 ms |
54364 KB |
Output is correct |
4 |
Correct |
8 ms |
54492 KB |
Output is correct |
5 |
Correct |
7 ms |
54616 KB |
Output is correct |
6 |
Correct |
7 ms |
54364 KB |
Output is correct |
7 |
Correct |
7 ms |
54508 KB |
Output is correct |
8 |
Correct |
8 ms |
54364 KB |
Output is correct |
9 |
Correct |
7 ms |
54364 KB |
Output is correct |
10 |
Correct |
8 ms |
54528 KB |
Output is correct |
11 |
Correct |
8 ms |
54620 KB |
Output is correct |
12 |
Correct |
7 ms |
54620 KB |
Output is correct |
13 |
Correct |
8 ms |
54624 KB |
Output is correct |
14 |
Correct |
11 ms |
54620 KB |
Output is correct |
15 |
Correct |
8 ms |
54604 KB |
Output is correct |
16 |
Correct |
7 ms |
54620 KB |
Output is correct |
17 |
Correct |
10 ms |
54552 KB |
Output is correct |
18 |
Correct |
7 ms |
54620 KB |
Output is correct |
19 |
Correct |
7 ms |
54620 KB |
Output is correct |
20 |
Correct |
7 ms |
54620 KB |
Output is correct |
21 |
Correct |
8 ms |
54616 KB |
Output is correct |
22 |
Correct |
7 ms |
54620 KB |
Output is correct |
23 |
Correct |
8 ms |
54620 KB |
Output is correct |
24 |
Correct |
10 ms |
54620 KB |
Output is correct |
25 |
Correct |
8 ms |
54620 KB |
Output is correct |
26 |
Correct |
8 ms |
54620 KB |
Output is correct |
27 |
Correct |
8 ms |
54620 KB |
Output is correct |
28 |
Correct |
9 ms |
54772 KB |
Output is correct |
29 |
Correct |
13 ms |
54620 KB |
Output is correct |
30 |
Correct |
9 ms |
54620 KB |
Output is correct |
31 |
Correct |
10 ms |
54620 KB |
Output is correct |
32 |
Correct |
9 ms |
54620 KB |
Output is correct |
33 |
Correct |
20 ms |
54620 KB |
Output is correct |
34 |
Correct |
24 ms |
54620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
54364 KB |
Output is correct |
2 |
Correct |
7 ms |
54364 KB |
Output is correct |
3 |
Correct |
7 ms |
54360 KB |
Output is correct |
4 |
Correct |
7 ms |
54492 KB |
Output is correct |
5 |
Correct |
8 ms |
54620 KB |
Output is correct |
6 |
Correct |
7 ms |
54616 KB |
Output is correct |
7 |
Correct |
7 ms |
54364 KB |
Output is correct |
8 |
Correct |
7 ms |
54364 KB |
Output is correct |
9 |
Correct |
8 ms |
54620 KB |
Output is correct |
10 |
Correct |
8 ms |
54620 KB |
Output is correct |
11 |
Correct |
8 ms |
54576 KB |
Output is correct |
12 |
Correct |
8 ms |
54620 KB |
Output is correct |
13 |
Correct |
8 ms |
54620 KB |
Output is correct |
14 |
Correct |
9 ms |
54624 KB |
Output is correct |
15 |
Correct |
8 ms |
54620 KB |
Output is correct |
16 |
Correct |
8 ms |
54620 KB |
Output is correct |
17 |
Correct |
10 ms |
54876 KB |
Output is correct |
18 |
Correct |
8 ms |
54616 KB |
Output is correct |
19 |
Correct |
7 ms |
54620 KB |
Output is correct |
20 |
Correct |
8 ms |
54620 KB |
Output is correct |
21 |
Correct |
7 ms |
54620 KB |
Output is correct |
22 |
Correct |
7 ms |
54620 KB |
Output is correct |
23 |
Correct |
8 ms |
54672 KB |
Output is correct |
24 |
Correct |
9 ms |
54628 KB |
Output is correct |
25 |
Correct |
8 ms |
54556 KB |
Output is correct |
26 |
Correct |
8 ms |
54616 KB |
Output is correct |
27 |
Correct |
8 ms |
54620 KB |
Output is correct |
28 |
Correct |
10 ms |
54620 KB |
Output is correct |
29 |
Correct |
14 ms |
54620 KB |
Output is correct |
30 |
Correct |
9 ms |
54540 KB |
Output is correct |
31 |
Correct |
12 ms |
54620 KB |
Output is correct |
32 |
Correct |
9 ms |
54620 KB |
Output is correct |
33 |
Correct |
21 ms |
54800 KB |
Output is correct |
34 |
Correct |
22 ms |
54620 KB |
Output is correct |
35 |
Correct |
19 ms |
55644 KB |
Output is correct |
36 |
Correct |
9 ms |
54748 KB |
Output is correct |
37 |
Correct |
26 ms |
55400 KB |
Output is correct |
38 |
Correct |
24 ms |
55644 KB |
Output is correct |
39 |
Correct |
26 ms |
55604 KB |
Output is correct |
40 |
Correct |
25 ms |
55644 KB |
Output is correct |
41 |
Correct |
25 ms |
55640 KB |
Output is correct |
42 |
Correct |
12 ms |
55456 KB |
Output is correct |
43 |
Correct |
11 ms |
55260 KB |
Output is correct |
44 |
Correct |
11 ms |
55208 KB |
Output is correct |
45 |
Correct |
68 ms |
56124 KB |
Output is correct |
46 |
Correct |
65 ms |
56148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
54364 KB |
Output is correct |
2 |
Correct |
7 ms |
54364 KB |
Output is correct |
3 |
Correct |
7 ms |
54456 KB |
Output is correct |
4 |
Correct |
8 ms |
54616 KB |
Output is correct |
5 |
Correct |
7 ms |
54364 KB |
Output is correct |
6 |
Correct |
7 ms |
54448 KB |
Output is correct |
7 |
Correct |
7 ms |
54364 KB |
Output is correct |
8 |
Correct |
7 ms |
54520 KB |
Output is correct |
9 |
Correct |
7 ms |
54364 KB |
Output is correct |
10 |
Correct |
7 ms |
54628 KB |
Output is correct |
11 |
Correct |
8 ms |
54616 KB |
Output is correct |
12 |
Correct |
7 ms |
54616 KB |
Output is correct |
13 |
Correct |
7 ms |
54620 KB |
Output is correct |
14 |
Correct |
8 ms |
54620 KB |
Output is correct |
15 |
Correct |
9 ms |
54872 KB |
Output is correct |
16 |
Correct |
7 ms |
54620 KB |
Output is correct |
17 |
Correct |
9 ms |
54620 KB |
Output is correct |
18 |
Correct |
7 ms |
54620 KB |
Output is correct |
19 |
Correct |
8 ms |
54620 KB |
Output is correct |
20 |
Correct |
7 ms |
54616 KB |
Output is correct |
21 |
Correct |
7 ms |
54620 KB |
Output is correct |
22 |
Correct |
7 ms |
54620 KB |
Output is correct |
23 |
Correct |
9 ms |
54616 KB |
Output is correct |
24 |
Correct |
9 ms |
54616 KB |
Output is correct |
25 |
Correct |
9 ms |
54644 KB |
Output is correct |
26 |
Correct |
9 ms |
54616 KB |
Output is correct |
27 |
Correct |
8 ms |
54620 KB |
Output is correct |
28 |
Correct |
10 ms |
54600 KB |
Output is correct |
29 |
Correct |
13 ms |
54620 KB |
Output is correct |
30 |
Correct |
10 ms |
54620 KB |
Output is correct |
31 |
Correct |
11 ms |
54872 KB |
Output is correct |
32 |
Correct |
10 ms |
54616 KB |
Output is correct |
33 |
Correct |
20 ms |
54616 KB |
Output is correct |
34 |
Correct |
20 ms |
54872 KB |
Output is correct |
35 |
Correct |
19 ms |
55644 KB |
Output is correct |
36 |
Correct |
10 ms |
54620 KB |
Output is correct |
37 |
Correct |
26 ms |
55472 KB |
Output is correct |
38 |
Correct |
25 ms |
55672 KB |
Output is correct |
39 |
Correct |
25 ms |
55636 KB |
Output is correct |
40 |
Correct |
27 ms |
55644 KB |
Output is correct |
41 |
Correct |
25 ms |
55612 KB |
Output is correct |
42 |
Correct |
12 ms |
55260 KB |
Output is correct |
43 |
Correct |
10 ms |
55304 KB |
Output is correct |
44 |
Correct |
10 ms |
55296 KB |
Output is correct |
45 |
Correct |
65 ms |
56148 KB |
Output is correct |
46 |
Correct |
65 ms |
56148 KB |
Output is correct |
47 |
Correct |
80 ms |
56156 KB |
Output is correct |
48 |
Correct |
13 ms |
55464 KB |
Output is correct |
49 |
Correct |
12 ms |
55380 KB |
Output is correct |
50 |
Correct |
10 ms |
55132 KB |
Output is correct |
51 |
Correct |
47 ms |
56664 KB |
Output is correct |
52 |
Correct |
55 ms |
56652 KB |
Output is correct |
53 |
Correct |
18 ms |
55640 KB |
Output is correct |
54 |
Correct |
8 ms |
54364 KB |
Output is correct |
55 |
Correct |
9 ms |
54536 KB |
Output is correct |
56 |
Correct |
14 ms |
55900 KB |
Output is correct |
57 |
Correct |
79 ms |
55008 KB |
Output is correct |
58 |
Correct |
14 ms |
55260 KB |
Output is correct |
59 |
Correct |
18 ms |
55252 KB |
Output is correct |
60 |
Correct |
25 ms |
55264 KB |
Output is correct |
61 |
Correct |
20 ms |
55256 KB |
Output is correct |
62 |
Correct |
44 ms |
55896 KB |
Output is correct |
63 |
Correct |
41 ms |
59588 KB |
Output is correct |
64 |
Correct |
42 ms |
58580 KB |
Output is correct |
65 |
Correct |
346 ms |
59856 KB |
Output is correct |
66 |
Correct |
881 ms |
58572 KB |
Output is correct |
67 |
Correct |
886 ms |
59604 KB |
Output is correct |