这道题是二分的板子题。
对于每个询问,我们可以用 STL 中的 upper_bound 来解决。
时间复杂度 。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int n,a[N],q;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>a[i];
while(q--){
int x;
cin>>x;
int pos=upper_bound(a+1,a+n+1,x)-a;
if(a[pos]<=x)cout<<-1<<endl;
else cout<<pos<<endl;
}
}
暂无评论