# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
683469 | nguyentunglam | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 223 ms | 262144 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 = 1e5 + 10, M = 1e7 + 10;
int b[N], p[N], cnt, f[M], d[N];
bool check[N], c[N];
pair<int, int> a[M];
vector<int> v[N], adj[M], g[N], tmp;
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;
int start = clock();
for(int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
p[i] = min(p[i], n);
g[p[i]].push_back(b[i]);
}
for(int val = 1; val <= n; val++) {
for(int j : g[val]) {
if (!c[j % val]) tmp.push_back(j % val), c[j % val] = 1;
}
for(int j : tmp) {
for(int k = j; k < n; k += val) {
d[k] = ++cnt;
if (cnt > M - 1) return 0;
a[cnt] = {k, val};
if (k >= val) {
adj[cnt].push_back(d[k - val]);
adj[d[k - val]].push_back(cnt);
}
}
}
tmp.clear();
for(int j : g[val]) v[j].push_back(d[j]), c[j % val] = 0;
g[val].clear();
}
int ans;
for(int i = 1; i <= cnt; i++) if (a[i].fi == b[1] && a[i].se == p[1]) ans = i;
queue<int> q;
for(int j : v[b[0]]) if (!f[j]) f[j] = 1, q.push(j);
check[b[0]] = 1;
while (!q.empty()) {
int id = q.front();
q.pop();
if (id == ans) return cout << f[ans] - 1, 0;
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]) if (!f[j]) f[j] = f[id] + 1, q.push(j);
}
if (u + w < n && !check[u + w]) {
check[u + w] = 1;
for(int j : v[u + w]) if (!f[j]) f[j] = f[id] + 1, q.push(j);
}
forv(v, adj[id]) if (!f[v]) f[v] = f[id] + 1, q.push(v);
}
cout << -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... |