# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
654329 | nguyentunglam | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 1089 ms | 56704 KiB |
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>
#define forin(i, a, b) for(int i = a; i <= b; i++)
#define forde(i, a, b) for(int i = a; i >= b; i--)
#define forv(a, b) for(auto & a : b)
#define fi first
#define se second
#define ii pair<int, int>
#define endl "\n"
using namespace std;
const int N = 3e4 + 10, M = 1e6;
int b[N], p[N], cnt, f[M];
bool check[N];
pair<int, int> a[M];
vector<int> v[N];
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int n, m; cin >> n >> m;
map<pair<int, int>, int> d;
for(int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
v[b[i]].push_back(p[i]);
if (!d[{b[i], p[i]}]) {
for(int j = b[i] % p[i]; j < n; j += p[i]) {
d[{j, p[i]}] = ++cnt;
a[cnt] = {j, p[i]};
}
}
}
// cout << cnt << endl;
// for(int i = 1; i <= cnt; i++) cout << a[i].fi << " " << a[i].se << endl;
queue<int> q;
for(int j : v[b[0]]) {
int id = d[{b[0], j}];
if (!f[id]) f[id] = 1, q.push(id);
}
check[b[0]] = 1;
while (!q.empty()) {
int id = q.front();
q.pop();
int u = a[id].fi, w = a[id].se;
if (u - w >= 0 && !check[u - w]) {
check[u - w] = 1;
for(int j : v[u - w]) {
int stt = d[{u - w, j}];
f[stt] = f[id] + 1, q.push(stt);
}
}
if (u + w < n && !check[u + w]) {
check[u + w] = 1;
for(int j : v[u + w]) {
int stt = d[{u + w, j}];
f[stt] = f[id] + 1, q.push(stt);
}
}
if (u - w >= 0) {
int stt = d[{u - w, w}];
if (!f[stt]) f[stt] = f[id] + 1, q.push(stt);
}
if (u + w < n) {
int stt = d[{u + w, w}];
if (!f[stt]) f[stt] = f[id] + 1, q.push(stt);
}
}
cout << f[d[{b[1], p[1]}]] - 1;
}
Compilation message (stderr)
# | 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... |