# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
70043 | antimirage | Railway Trip (JOI17_railway_trip) | C++17 | 2071 ms | 17204 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 <algorithm>
#include <iostream>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <iomanip>
#include <utility>
#include <math.h>
#include <time.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 1e5 + 5;
int n, k, q, ar[N], u[N], a, b, used[N], v;
vector <int> g[N];
queue <int> qu;
main()
{
cin >> n >> k >> q;
for (int i = 1; i <= n; i++)
scanf("%d", &ar[i]);
for (int i = 1; i <= n; i++)
{
for (int j = i - 1; j >= 1; j--)
{
if (ar[j] >= ar[i])
{
g[i].pb(j);
break;
}
}
for (int j = i + 1; j <= n; j++)
{
if (ar[j] >= ar[i])
{
g[i].pb(j);
break;
}
}
}
while (q--)
{
memset(u, -1, sizeof(u));
memset(used, -1, sizeof(used));
scanf("%d%d", &a, &b);
v = a;
qu.push(v);
u[v] = 0;
while (!qu.empty())
{
v = qu.front();
qu.pop();
for(auto to : g[v])
{
if ( u[to] == -1 )
{
qu.push(to);
u[to] = u[v] + 1;
}
}
}
int ans = (u[b] == -1 ? 1e9 + 7 : u[b] - 1);
v = b;
qu.push(v);
used[v] = 0;
while (!qu.empty())
{
v = qu.front();
qu.pop();
for(auto to : g[v])
{
if ( used[to] == -1 )
{
qu.push(to);
used[to] = used[v] + 1;
}
}
}
ans = (used[a] == -1 ? ans : used[a] - 1);
for (int i = 1; i <= n; i++)
{
if (used[i] != -1 && u[i] != -1)
ans = min(ans, used[i] + u[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... |