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>
using namespace std;
const int MAXN = 30010;
vector<int> p2bs[MAXN];
vector<int> col2i[MAXN];
vector<int> ans[MAXN];
int ps[MAXN], bs[MAXN];
int x2j(int id, int x){
int p = ps[id];
return x / p;
}
int& ans_at(int i, int x){
// cout << i << " " << x << endl;
// cout << ans[i + 1].size() << endl;
// if(i > 0)cout << x2j(i, x) << endl;
if(i == -1)
return ans[i + 1][x];
else
return ans[i + 1][x2j(i, x)];
}
int j2x(int id, int j){
int p = ps[id];
int b = bs[id];
return (b % p) + p * j;
}
int32_t main(){
cin.tie(NULL)->sync_with_stdio(false);
int n, m; cin >> n >> m;
int B[2], P[2];
cin >> B[0] >> P[0];
cin >> B[1] >> P[1];
vector<pair<int,int>> v;
for(int i = 2; i < m; ++i){
int b, p; cin >> b >> p;
v.push_back({p, b});
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
ans[0] = vector<int>(n, INT_MAX);
for(int i = 0; i < v.size(); ++i){
auto [p, b] = v[i];
ps[i] = p, bs[i] = b;
for(int j = b % p; j < n; j += p){
ans[i+1].push_back(INT_MAX);
}
col2i[b].push_back(i);
}
struct Node{
int i, x;
// se i == -1, eh col
int cost;
bool operator<(const Node &n) const{
return cost > n.cost;
}
};
priority_queue<Node> pq;
deque<Node> q;
for(int i = (B[0] % P[0]); i < n; i += P[0]){
int dist = abs(i - B[0]) / P[0];
pq.push({-1, i, ans_at(-1, i) = dist});
}
// print();
while(pq.size() + q.size() > 0){
auto maybe_update = [&](int p, int x, int val, int d){
if(ans_at(p, x) > val){
Node X = {p, x, ans_at(p, x) = val};
if(d == 0) q.push_front(X);
else if(d == 1) q.push_back(X);
else pq.push(X);
}
};
Node nxt;
{
if(q.size()){
nxt = q.front();
q.pop_front();
}else{
nxt = pq.top(); pq.pop();
}
}
auto [i, x, cost] = nxt;
if(cost > ans_at(i, x)) continue;
// cout << i << " " << x << " " << cost << "\n";
if(i == -1){
for(auto i : col2i[x]){
maybe_update(i, x, cost, 0);
}
}else{
// sou power i na coluna x
maybe_update(-1, x, cost, 0);
if(x + ps[i] < n){
maybe_update(i, x + ps[i], cost + 1, 1);
}
if(x - ps[i] >= 0){
maybe_update(i, x - ps[i], cost + 1, 1);
}
}
// print();
}
cout << ((ans_at(-1, B[1]) == INT_MAX)? -1 : ans_at(-1, B[1])) << "\n";
}
Compilation message (stderr)
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:53:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0; i < v.size(); ++i){
| ~~^~~~~~~~~~
skyscraper.cpp:54:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | auto [p, b] = v[i];
| ^
skyscraper.cpp:104:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
104 | auto [i, x, cost] = nxt;
| ^
# | 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... |