#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
int ar[n];
vector<pair<int, int> > li;
for(int i = 0; i<n; i++){
cin >> ar[i];
}
int left[n];
left[0] = 0;
li.push_back(make_pair(ar[0],0));
for(int i= 1; i<n; i++){
while(li[(int)li.size()-1].first<ar[i]){
li.pop_back();
}
left[i] = li[(int)li.size()-1].second;
li.push_back(make_pair(ar[i],i));
}
li.clear();
int right[n];
right[n-1] = n-1;
li.push_back(make_pair(ar[n-1],n-1));
for(int i = n-2; i>=0; i--){
while(li[(int)li.size()-1].first<ar[i]){
li.pop_back();
}
right[i] = li[(int)li.size()-1].second;
li.push_back(make_pair(ar[i],i));
}
for(int i = 0; i<q; i++){
int s, t;
cin >> s >> t;
s--;
t--;
int dist1[n];
int dist2[n];
int inf = n+2;
for(int j = 0; j<n; j++){
dist1[j] = inf;
dist2[j] = inf;
}
vector<int> li1;
dist1[s] = 0;
li1.push_back(s);
for(int j = 0; j<li1.size(); j++){
int now = li1[j];
if(dist1[left[now]]==inf){
dist1[left[now]] = dist1[now]+1;
li1.push_back(left[now]);
}
if(dist1[right[now]]==inf){
dist1[right[now]] = dist1[now]+1;
li1.push_back(right[now]);
}
}
vector<int> li2;
dist2[t] = 0;
li2.push_back(t);
for(int j = 0; j<li2.size(); j++){
int now = li2[j];
if(dist2[left[now]]==inf){
dist2[left[now]] = dist2[now]+1;
li2.push_back(left[now]);
}
if(dist2[right[now]]==inf){
dist2[right[now]] = dist2[now]+1;
li2.push_back(right[now]);
}
}
int ans = inf;
for(int i = 0; i<n; i++){
ans = min(ans,dist1[i] + dist2[i] - 1);
}
cout << ans << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
3 ms |
500 KB |
Output is correct |
8 |
Correct |
2 ms |
704 KB |
Output is correct |
9 |
Correct |
2 ms |
752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
752 KB |
Output is correct |
2 |
Correct |
38 ms |
3148 KB |
Output is correct |
3 |
Correct |
37 ms |
3264 KB |
Output is correct |
4 |
Correct |
36 ms |
3400 KB |
Output is correct |
5 |
Correct |
32 ms |
3700 KB |
Output is correct |
6 |
Correct |
37 ms |
4156 KB |
Output is correct |
7 |
Correct |
35 ms |
4640 KB |
Output is correct |
8 |
Correct |
140 ms |
7320 KB |
Output is correct |
9 |
Correct |
75 ms |
7320 KB |
Output is correct |
10 |
Correct |
81 ms |
7812 KB |
Output is correct |
11 |
Correct |
87 ms |
7916 KB |
Output is correct |
12 |
Correct |
104 ms |
8456 KB |
Output is correct |
13 |
Correct |
98 ms |
9004 KB |
Output is correct |
14 |
Correct |
82 ms |
9600 KB |
Output is correct |
15 |
Correct |
87 ms |
10156 KB |
Output is correct |
16 |
Correct |
80 ms |
10716 KB |
Output is correct |
17 |
Correct |
35 ms |
10716 KB |
Output is correct |
18 |
Correct |
32 ms |
10716 KB |
Output is correct |
19 |
Correct |
32 ms |
11120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2060 ms |
12072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2076 ms |
12072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |