#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, q;
cin >> n >> k >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> g(n);
vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && a[stk.back()] < a[i]) {
stk.pop_back();
}
if (!stk.empty()) {
g[i].push_back(stk.back());
g[stk.back()].push_back(i);
}
stk.push_back(i);
}
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && a[stk.back()] < a[i]) {
stk.pop_back();
}
if (!stk.empty()) {
g[i].push_back(stk.back());
g[stk.back()].push_back(i);
}
stk.push_back(i);
}
while (q--) {
int a, b;
cin >> a >> b;
--a; --b;
vector<int> dist(n, -1);
vector<int> que(1, a);
dist[a] = 0;
for (int b = 0; b < (int) que.size(); b++) {
int x = que[b];
for (int y : g[x]) {
if (dist[y] == -1) {
dist[y] = dist[x] + 1;
que.push_back(y);
}
}
}
cout << dist[b] - 1 << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
145 ms |
9264 KB |
Output is correct |
3 |
Correct |
153 ms |
9232 KB |
Output is correct |
4 |
Correct |
168 ms |
9204 KB |
Output is correct |
5 |
Correct |
155 ms |
9604 KB |
Output is correct |
6 |
Correct |
150 ms |
9332 KB |
Output is correct |
7 |
Correct |
152 ms |
9488 KB |
Output is correct |
8 |
Correct |
58 ms |
8224 KB |
Output is correct |
9 |
Correct |
68 ms |
9260 KB |
Output is correct |
10 |
Correct |
63 ms |
8892 KB |
Output is correct |
11 |
Correct |
140 ms |
9636 KB |
Output is correct |
12 |
Correct |
142 ms |
9696 KB |
Output is correct |
13 |
Correct |
148 ms |
9784 KB |
Output is correct |
14 |
Correct |
141 ms |
9652 KB |
Output is correct |
15 |
Correct |
136 ms |
9652 KB |
Output is correct |
16 |
Correct |
136 ms |
9652 KB |
Output is correct |
17 |
Correct |
151 ms |
9696 KB |
Output is correct |
18 |
Correct |
161 ms |
9536 KB |
Output is correct |
19 |
Correct |
82 ms |
8920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2057 ms |
9476 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2044 ms |
10108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |