#include <bits/stdc++.h>
#define ll long long
#define all(lpv) lpv.begin(), lpv.end()
#define pot(x, y) lower_bound(x.begin(), x.end(), y) - x.begin() + 1
using namespace std;
#define lpv
#ifndef lpv
#include "AC.h"
#endif // lpv
//#define int long long
typedef tuple<int,int,int> tp;
const int N = 3e4 + 2;
const int block = 173;
const int oo = 2e9;
int n, m, b[N], p[N];
int f[block + 2][N];
vector<int> col[N];
#ifdef lpv
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")) {
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> m;
for(int i = 1; i <= m; i ++) {
cin >> b[i] >> p[i];
b[i]++;
col[b[i]].push_back(i);
}
priority_queue<tp, vector<tp>, greater<tp>> pq;
for(int i = 1; i <= n; i ++) {
for(int k = 0; k <= block; k ++) f[k][i] = oo;
}
pq.push({f[0][b[1]] = 0, 0, b[1]});
while(!pq.empty()) {
int w, t, u; tie(w, t, u) = pq.top(); pq.pop();
if(w > f[t][u]) continue;
if(f[0][u] > f[t][u]) {
pq.push({f[0][u] = f[t][u], 0, u});
}
if(t) {
if(u - t >= 1 && f[t][u - t] > w + 1) {
pq.push({f[t][u - t] = w + 1, t, u - t});
}
if(u + t <= n && f[t][u + t] > w + 1) {
pq.push({f[t][u + t] = w + 1, t, u + t});
}
continue;
}
for(auto i : col[u]) {
if(p[i] > block) {
for(int j = u + p[i], cnt = 1; j <= n; j += p[i], cnt ++) if(f[0][j] > w + cnt) {
pq.push({f[0][j] = w + cnt, 0, j});
}
for(int j = u - p[i], cnt = 1; j >= 1; j -= p[i], cnt ++) if(f[0][j] > w + cnt) {
pq.push({f[0][j] = w + cnt, 0, j});
}
} else {
if(u - p[i] >= 1 && f[p[i]][u - p[i]] > w + 1) {
pq.push({f[p[i]][u - p[i]] = w + 1, p[i], u - p[i]});
}
if(u + p[i] <= n && f[p[i]][u + p[i]] > w + 1) {
pq.push({f[p[i]][u + p[i]] = w + 1, p[i], u + p[i]});
}
}
}
}
int ans = oo;
for(int i = 0; i <= block; i ++) ans = min(ans, f[i][b[2]]);
if(ans == oo) cout << -1 ;
else cout << ans;
}
#endif // lpv
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:31:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |