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>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) begin(x), end(x)
#define SZ(x) (int)(x).size()
#define cps(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int n0 = 30003, inf = 1e9, k0 = 7500000;
int n, m, num, ind[n0];
vector <int> rem[n0], vec[n0], out[n0];
int g1[k0], g2[k0], g3[k0], b[k0], d[k0];
deque <int> q;
void relax0(int v, int u) {
if (d[u] > d[v]) {
d[u] = d[v];
q.push_front(u);
}
}
void relax1(int v, int u) {
if (d[u] > d[v] + 1) {
d[u] = d[v] + 1;
q.pb(u);
}
}
void bfs(int x) {
q.pb(x);
for (int i = 0; i < num; i++)
d[i] = inf;
d[x] = 0;
while (!q.empty()) {
int v = q.front();
q.pop_front();
if (v < n) {
for (int i : out[v]) relax0(v, i);
} else {
if (g1[v]) relax1(v, v - 1);
if (g2[v]) relax1(v, v + 1);
relax0(v, g3[v]);
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> n >> m;
int s, f;
for (int i = 0; i < m; i++) {
int B, P;
cin >> B >> P;
if (i == 0) s = B;
if (i == 1) f = B;
vec[P].pb(B);
rem[P].pb(B % P);
}
num = n;
for (int i = 1; i < n0; i++) {
cps(rem[i]);
for (int j : rem[i])
num += ((n - 1) - j) / i + 1;
}
assert(num < 7500000);
int cur = n;
for (int i = 1; i < n0; i++) {
for (int j : rem[i]) {
for (int k = j; k < n; k += i) {
g1[cur] = (k != j);
g2[cur] = (k + i < n);
g3[cur] = k;
ind[k] = cur++;
}
}
cps(vec[i]);
for (int j : vec[i])
out[j].pb(ind[j]);
}
bfs(s);
if (d[f] == inf) cout << -1;
else cout << d[f];
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:83:9: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | if (d[f] == inf) cout << -1;
| ~~~^
skyscraper.cpp:82:5: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
82 | bfs(s);
| ~~~^~~
# | 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... |