#include <deque>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct edge {
int to, cost;
edge() : to(-1), cost(-1) {};
edge(int to_, int cost_) : to(to_), cost(cost_) {};
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> B(M), P(M);
vector<vector<int> > vmod(N);
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
vmod[P[i]].push_back(B[i]);
}
vector<vector<edge> > g(N);
for (int i = 1; i < N; i++) {
sort(vmod[i].begin(), vmod[i].end(), [&](int j, int k) { return j % i != k % i ? j % i < k % i : j < k; });
vmod[i].erase(unique(vmod[i].begin(), vmod[i].end()), vmod[i].end());
int pl = 0;
for (int j = 1; j <= vmod[i].size(); j++) {
if (j != vmod[i].size() && vmod[i][j] % i == vmod[i][j - 1] % i) continue;
int pre = -1;
for (int k = vmod[i][j - 1] % i; k < N; k += i) {
if (pre != -1) g[pre].push_back(edge(g.size(), 1));
g.push_back(pre == -1 ? vector<edge>({ edge(k, 0) }) : vector<edge>({ edge(k, 0), edge(pre, 1) }));
pre = g.size() - 1;
while (pl != j && vmod[i][pl] < k) pl++;
if (pl != j && vmod[i][pl] == k) g[k].push_back(edge(pre, 0));
}
pl = j;
}
}
vector<int> dist(g.size(), -1); dist[B[0]] = 0;
deque<int> que; que.push_back(B[0]);
while (!que.empty()) {
int u = que.front(); que.pop_front();
for (edge e : g[u]) {
if (dist[e.to] == -1) {
dist[e.to] = dist[u] + e.cost;
if (e.cost == 0) que.push_front(e.to);
else que.push_back(e.to);
}
}
}
cout << dist[B[1]] << '\n';
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 1; j <= vmod[i].size(); j++) {
~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:28:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j != vmod[i].size() && vmod[i][j] % i == vmod[i][j - 1] % i) continue;
~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Runtime error |
4 ms |
616 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Runtime error |
5 ms |
828 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
828 KB |
Output is correct |
2 |
Runtime error |
4 ms |
828 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
828 KB |
Output is correct |
2 |
Runtime error |
4 ms |
988 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1072 KB |
Output is correct |
2 |
Runtime error |
4 ms |
1072 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |