//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
int n,k,q;
int it[100001];
int dis[100001];
vector<int> adj[100001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>k>>q;
for(int i=0;i<n;i++){
cin>>it[i];
}
/*for(int i=0;i<n;i++){
cout<<it[i]<<",";
}
cout<<endl;*/
if(true){
vector<pair<int,int>> ss;
for(int i=0;i<n;i++){
while(ss.size()){
if(ss.back().a<it[i]){
ss.pop_back();
continue;
}
else{
break;
}
}
if(ss.size()){
adj[i].pb(ss.back().b);
//cout<<i<<","<<ss.back().b<<endl;
}
ss.pb({it[i],i});
}
ss.clear();
for(int i=n-1;i>=0;i--){
while(ss.size()){
if(ss.back().a<it[i]){
ss.pop_back();
continue;
}
else{
break;
}
}
if(ss.size()){
adj[i].pb(ss.back().b);
// cout<<i<<":"<<ss.back().b<<endl;
}
ss.pb({it[i],i});
}
}
while(q--){
int aa,bb;
cin>>aa>>bb;
aa--;
bb--;
if(it[aa]>it[bb]){
swap(aa,bb);
}
queue<int> ss;
ss.push(aa);
for(int i=0;i<n;i++){
dis[i]=-1;
}
dis[aa]=0;
while(ss.size()){
int no=ss.front();
ss.pop();
for(auto j:adj[no]){
if(dis[j]==-1){
dis[j]=dis[no]+1;
ss.push(j);
}
}
}
cout<<dis[bb]-1<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2076 ms |
7276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2089 ms |
8172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |