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 = 4444444;
vector<int> G[MAX];
int D[MAX];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M, S, E;
cin >> N >> M;
map<pii, vector<int>> make;
for (int i = 0; i < M; ++i) {
int b, p;
cin >> b >> p;
if (i == 0) S = b;
if (i == 1) E = b;
make[{p, b % p}].push_back(b);
}
int node = N;
for (auto& [q, v] : make) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
auto [p, i] = q;
vector<int> fr;
for (int j = i; j < N; j += p) fr.push_back(j);
int base = -1;
for (int n = 0; n < fr.size(); ++n) {
G[node].push_back(fr[n]);
if (base != -1) G[base].push_back(node);
base = node++;
}
base = -1;
for (int n = fr.size() - 1; n >= 0; --n) {
G[node].push_back(fr[n]);
if (base != -1) G[base].push_back(node);
base = node++;
}
for (int s : v) {
int n = (s - i) / p;
if (n) G[s].push_back(fr[n - 1]);
if (n + 1 < fr.size()) G[s].push_back(fr[n + 1]);
if (1 < n) G[s].push_back(node - (n - 1));
if (n + 2 < fr.size()) G[s].push_back(node - 2 * fr.size() + n + 2);
}
}
make.clear();
memset(D, 0x3f, sizeof(D));
D[S] = 0;
queue<int> Q;
Q.push(S);
while (!Q.empty()) {
int cur = Q.front(); Q.pop();
for (int next : G[cur]) {
if (D[next] < 1e9) continue;
D[next] = D[cur] + 1;
Q.push(next);
}
}
cout << (D[E] > 1E9 ? -1 : D[E]);
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:23:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for (auto& [q, v] : make) {
| ^
skyscraper.cpp:26:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | auto [p, i] = q;
| ^
skyscraper.cpp:30:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int n = 0; n < fr.size(); ++n) {
| ~~^~~~~~~~~~~
skyscraper.cpp:44:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if (n + 1 < fr.size()) G[s].push_back(fr[n + 1]);
| ~~~~~~^~~~~~~~~~~
skyscraper.cpp:46:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if (n + 2 < fr.size()) G[s].push_back(node - 2 * fr.size() + n + 2);
| ~~~~~~^~~~~~~~~~~
skyscraper.cpp:64:14: warning: 'E' may be used uninitialized in this function [-Wmaybe-uninitialized]
64 | cout << (D[E] > 1E9 ? -1 : D[E]);
| ~~~^
# | 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... |