# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
549007 | LucaDantas | Railway Trip (JOI17_railway_trip) | C++17 | 2059 ms | 3284 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>
using namespace std;
constexpr int maxn = 1e5+10;
int a[maxn], l[maxn], r[maxn], dist[2][maxn];
void bfs(int s, int k) {
for(int i = 0; i < maxn; i++)
dist[k][i] = 0x3f3f3f3f;
dist[k][s] = 0;
queue<int> q;
q.push(s);
while(q.size()) {
int u = q.front(); int d = dist[k][u];
q.pop();
for(int rep = 0; rep < 2; rep++, swap(l[u], r[u]))
if(dist[k][l[u]] > d+1)
q.push(l[u]), dist[k][l[u]] = d+1;
}
}
int main() {
int n, k, q; scanf("%d %d %d", &n, &k, &q);
for(int i = 0; i < n; i++)
scanf("%d", a+i);
l[0] = 0;
for(int i = 1; i < n; i++) {
l[i] = i-1;
while(a[l[i]] < a[i])
l[i] = l[l[i]];
}
r[n-1] = n-1;
for(int i = n-2; i >= 0; i--) {
r[i] = i+1;
while(a[r[i]] < a[i])
r[i] = r[r[i]];
}
while(q--) {
int x, y; scanf("%d %d", &x, &y);
--x, --y;
bfs(x, 0); bfs(y, 1);
int ans = 0x3f3f3f3f;
for(int i = 0; i < n; i++)
ans = min(ans, dist[0][i] + dist[1][i] - 1);
printf("%d\n", ans);
}
}
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... |