#include <bits/stdc++.h>
using namespace std;
/*⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⠀⠀⠀⠀⠀⠀⠀⡄⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⣿⠛⣿⠀⠀⠀⠀⣤⣿⢻⡇⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⣿⡛⠀⣤⣿⣿⣤⣤⣿⣿⣤⢸⡇⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀
⠀⠀⠀⠀⠀⠀⠀⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡗⠀
⢠⣼⣿⣿⣿⣿⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷
⢸⣿⣿⡟⠛⠛⢿⣿⣿⣿⣿⣿⣿⣿⣤⣤⣤⣿⣿⣿⣿⣤⣤⣼⣿⣿
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠋
*/
#define fi first
#define se second
#define pb push_back
#define ins insert
#define sz(a) (int)(a.size())
#define all(x) (x).begin(),(x).end()
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define debug(...)
#endif
//const int mod = 1e9+7;
//const int mod = 998244353;
const int MAXN=3005;
const ll inf=1e9,INF=1e18;
int n,L[MAXN],R[MAXN];
int go(int st,int end) {
if(st == end) return 1;
int p = 2;
if(st < end) {
while(R[st] < end) {
int c = st;
for(int i=L[st];i<=R[st];i++) {
if(R[i] > R[c]) c = i;
}
if(st == c) return inf;
st = c; p++;
} return p;
} else {
while(L[st] > end) {
int c = st;
for(int i=R[st];i>=L[st];i--) {
if(L[i] < L[c]) c = i;
}
if(st == c) return inf;
st = c; p++;
} return p;
}
}
void solve(int tc = 0){
cin>>n;
for(int i=1;i<=n;i++) cin>>L[i]>>R[i];
int q; cin>>q;
while(q--) {
int x,ans = inf; cin>>x;
for(int i=1;i<=x;i++) {
if(L[i] == 1) {
for(int j=1;j<=i;j++) {
ans = min(go(x,i) + go(j,n) - 1 - (j == i),ans);
}
}
}
for(int i=x;i<=n;i++) {
if(R[i] == n) {
for(int j=i;j<=n;j++) {
ans = min(go(x,i) + go(j,1) - 1 - (j == i),ans);
}
}
}
print(ans > n ? -1 : ans);
}
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int tc=1;
//cin>>tc;
for(int t = 0; t < tc; t++) solve(t);
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |