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>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);
int n,k,q;
cin>>n>>k>>q;
vector <int> l(n);
for(int i=0;i<n;i++)cin>>l[i];
vector <int> g[n];
for(int i=0;i<n;i++){
int mx=0;
for(int j=i+1;j<n;j++){
if(l[j]>mx){
mx=l[j];
g[i].pb(j);
g[j].pb(i);
}
if(mx>=l[i])break;
}
}
while(q--){
int a,b;
cin>>a>>b;
a--;
b--;
set <pair <int,int> > st;
vector <int> d(n,1e18);
st.insert({0,a});
d[a]=0;
while(!st.empty()){
int v=st.begin()->second;
st.erase(st.begin());
for(auto to : g[v]){
if(d[to]>d[v]+1){
st.erase({d[to],to});
d[to]=d[v]+1;
st.insert({d[to],to});
}
}
}
int x=d[b]-1;
cout<<x<<"\n";
}
}
# | 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... |