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;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2007;
int n, k, q;
int a[N];
vector <int> g[N];
void bfs(int S, int dist[N]) {
queue <int> q;
for (int i = 0; i < N; ++i)
dist[i] = N;
q.push(S);
dist[S] = 0;
while (q.size()) {
int u = q.front(); q.pop();
for (int v : g[u]) {
if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
}
int dist[N][N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> k >> q;
if (n >= N)
exit(1);
for (int i = 1; i <= n; ++i)
cin >> a[i];
set <ii> p;
for (int i = 1; i <= n; ++i) {
int cur = -1;
int l = -1, r = -1;
for (int j = i - 1; j >= 0; --j) {
if (a[j] >= a[i]) {
l = j;
break;
}
}
cur = -1;
for (int j = i + 1; j <= n; ++j) {
if (a[j] >= a[i]) {
r = j;
break;
}
}
if (l != -1) {
p.insert(mp(l, i));
if (r == -1 || a[l] >= a[r]) {
g[i].app(l);
}
}
if (r != -1) {
p.insert(mp(i, r));
if (l == -1 || a[r] >= a[l]) {
g[i].app(r);
}
}
}
for (int i = 1; i <= n; ++i) {
bfs(i, dist[i]);
}
while (q--) {
int i, j;
cin >> i >> j;
if (j < i)
swap(i, j);
if (p.find(mp(i, j)) != p.end()) {
cout << 0 << endl;
continue;
}
int ans = N;
for (int k = 1; k <= n; ++k) {
ans = min(ans, dist[i][k] + dist[j][k]);
}
cout << ans - 1 << endl;
}
}
# | 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... |