This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
//#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int C = 150;
bool seen[30005][205];
int N, M, st, ed, pos, iq, k, dist[30005][205];
vector<iii> adj[30005][205];
priority_queue<iii, vector<iii>, greater<iii> > pq;
queue<ii> dial[205];
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 1; j < C; j++) {
if (i + j < N) adj[i][j].eb(i + j, j, 1);
if (i - j >= 0) adj[i][j].eb(i - j, j, 1);
dist[i][j] = 1e9;
adj[i][j].eb(i, 0, 0);
}
dist[i][0] = 1e9;
}
for (int i = 0, B, P; i < M; i++) {
cin >> B >> P;
if (P >= C) {
for (int j = B + P; j < N; j += P)
adj[B][0].eb(j, 0, (j - B) / P);
for (int j = B - P; j >= 0; j -= P)
adj[B][0].eb(j, 0, (B - j) / P);
} else {
adj[B][0].eb(B, P, 0);
}
if (i == 0) st = B;
else if (i == 1) ed = B;
}
dial[0].emplace(st, 0);
dist[st][0] = 0;
iq = 1;
k = N / C + 1;
while (iq) {
while (dial[pos].empty()) pos = (pos + 1) % (k + 1);
auto [x, y] = dial[pos].front();
dial[pos].pop();
iq--;
if (seen[x][y]) continue;
seen[x][y] = 1;
for (auto [u, v, w] : adj[x][y]) {
if (dist[u][v] > dist[x][y] + w) {
dist[u][v] = dist[x][y] + w;
iq++;
dial[dist[u][v] % (k + 1)].emplace(u, v);
}
}
}
cout << (dist[ed][0] == 1e9 ? -1 : dist[ed][0]) << '\n';
}
Compilation message (stderr)
skyscraper.cpp:35:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
35 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |