#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <functional>
using namespace std;
int N, M, A[30009], P[30009], dist[5300009]; vector<pair<int, int>>vec, X[5300009]; 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.begin(), vec.end(), make_pair(px, py)) - vec.begin();
int pos2 = lower_bound(vec.begin(), vec.end(), make_pair(qx, qy)) - vec.begin();
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.push_back(make_pair(j, P[i]));
}
}
for (int i = 0; i < N; i++) vec.push_back(make_pair(i, 0));
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); 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 < vec.size(); i++) dist[i] = (1 << 30);
int sx = lower_bound(vec.begin(), vec.end(), make_pair(A[1], 0)) - vec.begin();
int gx = lower_bound(vec.begin(), vec.end(), make_pair(A[2], 0)) - vec.begin();
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:27:2: error: 'sort' was not declared in this scope
sort(vec.begin(), vec.end());
^~~~
skyscraper.cpp:27:2: note: suggested alternative: 'qsort'
sort(vec.begin(), vec.end());
^~~~
qsort
skyscraper.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i++) {
~~^~~~~~~~~~~~
skyscraper.cpp:33: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:41:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i++) dist[i] = (1 << 30);
~~^~~~~~~~~~~~
skyscraper.cpp:49:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X[pos].size(); i++) {
~~^~~~~~~~~~~~~~~