이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
template<class K,class V> using ht = gp_hash_table<
K,
V,
hash<K>,
equal_to<K>,
direct_mask_range_hashing<>,
linear_probe_fn<>,
hash_standard_resize_policy<
hash_exponential_size_policy<>,
hash_load_check_resize_trigger<>,
true
>
>;
const int N = 3e4 + 100, INF = 1e9;
vector<int> P[N];
ht<int, int> jumps[N];
int sz[N];
set<ii> s;
void solve() {
int n, m;
cin >> n >> m;
vector<int> cnt(n, INF);
queue<ii> q;
int x0 = -1, x1 = -1;
for(int i = 0; i < m; ++i) {
int b, p;
cin >> b >> p;
if(!i) x0 = b;
else if(i == 1) x1 = b;
if(!s.count(ii(b%p,p))) {
s.insert(ii(b%p,p));
for(int x=b%p;x<n;x+=p)++sz[x];
}
P[b].push_back(p);
}
for(int i=0;i<n;++i) jumps[i].resize(sz[i]);
while(!P[x0].empty()) {
int k = P[x0].back();
P[x0].pop_back();
if(jumps[x0][k] == 0) {
jumps[x0][k] = 1;
q.emplace(x0, k);
}
}
while(!q.empty()) {
auto [x, p] = q.front();
q.pop();
int j = jumps[x][p];
cnt[x] = min(cnt[x], j);
if(x == x1) break;
if(x >= p && jumps[x - p][p] == 0) {
jumps[x - p][p] = 1 + j;
q.emplace(x - p, p);
}
if(x + p < n && jumps[x + p][p] == 0) {
jumps[x + p][p] = 1 + j;
q.emplace(x + p, p);
}
while(!P[x].empty()) {
int k = P[x].back();
P[x].pop_back();
if(x >= k && jumps[x - k][k] == 0) {
jumps[x - k][k] = 1 + j;
q.emplace(x - k, k);
}
if(x + k < n && jumps[x + k][k] == 0) {
jumps[x + k][k] = 1 + j;
q.emplace(x + k, k);
}
}
}
if(cnt[x1] == INF) cout << "-1\n";
else cout << cnt[x1] - 1 << '\n';
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
# | 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... |