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>
#define pii pair<int, int>
using namespace std;
const int MAX = 5555555;
vector<int> G[MAX];
vector<int> C[30003];
bitset<MAX> noL, noR;
int D[MAX];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M;
cin >> N >> M;
map<pii, vector<int>> make;
pii S, E;
for (int i = 0; i < M; ++i) {
int b, p;
cin >> b >> p; b++;
if (i == 0) S = { b, p };
if (i == 1) E = { b, p };
make[{p, b % p}].push_back(b);
}
int node = N + 1, SN, EN;
for (auto& [q, v] : make) {
auto [p, i] = q;
if (!i) i += p;
vector<int> fr;
for (int j = i; j <= N; j += p) {
if (S == pii{ j, p }) SN = node;
if (E == pii{ j, p }) EN = node;
C[j].push_back(node);
fr.push_back(node++);
}
noL[fr[0]] = noR[fr.back()] = 1;
//for (int j = 1; j < fr.size(); ++j) {
// G[fr[j - 1]].emplace_back(fr[j]);
// G[fr[j]].emplace_back(fr[j - 1]);
//}
for (int n : v) {
G[n].push_back(-fr[(n - 1) / p]);
}
}
make.clear();
for (int i = 1; i <= N; ++i) {
for (int n : C[i]) {
G[n].push_back(-i);
}
C[i].clear();
}
memset(D, 0x3f, sizeof(D));
D[SN] = 0;
deque<int> Q;
Q.push_back(SN);
while (!Q.empty()) {
int cur = Q.front(); Q.pop_front();
vector<int> next2;
if (N < cur) {
if (!noL[cur]) next2.push_back(cur - 1);
if (!noR[cur]) next2.push_back(cur + 1);
}
for (int n : next2) G[cur].push_back(n);
for (int next : G[cur]) {
int d = 1;
if (next < 0) {
d = 0;
next *= -1;
}
if (D[cur] + d >= D[next]) continue;
D[next] = D[cur] + d;
if (d == 0) Q.push_front(next);
else Q.push_back(next);
}
for (int i = 0; i < next2.size(); ++i) G[cur].pop_back();
}
cout << (D[EN] > 1E9 ? -1 : D[EN]);
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:26:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for (auto& [q, v] : make) {
| ^
skyscraper.cpp:27:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
27 | auto [p, i] = q;
| ^
skyscraper.cpp:80:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 0; i < next2.size(); ++i) G[cur].pop_back();
| ~~^~~~~~~~~~~~~~
skyscraper.cpp:83:15: warning: 'EN' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | cout << (D[EN] > 1E9 ? -1 : D[EN]);
| ~~~~^
# | 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... |