#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
#include <cmath>
using namespace std;
int N, M, A[30009], P[30009], dist[7400009], sz; pair<int, int>vec[7400009]; vector<pair<int, int>>X[7400009]; vector<int> G[30009];
map<pair<int, int>, int>M1; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>Q;
void connect(int px, int py, int qx, int qy, int cost) {
int pos1 = lower_bound(vec, vec + sz, make_pair(px, py)) - vec;
int pos2 = lower_bound(vec, vec + sz, make_pair(qx, qy)) - vec;
X[pos1].push_back(make_pair(pos2, cost));
}
int main() {
cin >> N >> M;
for (int i = 1; i <= M; i++) {
cin >> A[i] >> P[i]; G[A[i]].push_back(P[i]);
if (M1[make_pair(A[i] % P[i], P[i])] == 0) {
M1[make_pair(A[i] % P[i], P[i])] = 1;
for (int j = A[i] % P[i]; j < N; j += P[i]) { vec[sz] = make_pair(j, P[i]); sz++; }
}
}
for (int i = 0; i < N; i++) { vec[sz] = make_pair(i, 0); sz++; }
sort(vec, vec + sz);
for (int i = 0; i < sz; i++) {
int pos = vec[i].first, state = vec[i].second;
if (state == 0) {
for (int j = 0; j < G[pos].size(); j++) connect(pos, 0, pos, G[pos][j], 0);
}
else {
connect(pos, state, pos, 0, 0);
if (pos - state >= 0) connect(pos, state, pos - state, state, 1);
if (pos + state < N) connect(pos, state, pos + state, state, 1);
}
}
for (int i = 0; i < sz; i++) dist[i] = (1 << 30);
int sx = lower_bound(vec, vec + sz, make_pair(A[1], 0)) - vec;
int gx = lower_bound(vec, vec + sz, make_pair(A[2], 0)) - vec;
dist[sx] = 0; Q.push(make_pair(dist[sx], sx));
while (!Q.empty()) {
int pos = Q.top().second; Q.pop();
for (int i = 0; i < X[pos].size(); i++) {
int to = X[pos][i].first, cost = X[pos][i].second;
if (dist[to] > dist[pos] + cost) {
dist[to] = dist[pos] + cost;
Q.push(make_pair(dist[to], to));
}
}
}
int ans = dist[gx]; if (ans == (1 << 30)) ans = -1;
cout << ans << endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:35:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[pos].size(); j++) connect(pos, 0, pos, G[pos][j], 0);
~~^~~~~~~~~~~~~~~
skyscraper.cpp:51:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X[pos].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
256 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
352 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
352 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
352 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
352 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |