logo AlgoBeat OnlineJudge
登录 注册

题解

作者: _ZXY_  ·  发布于 2026-05-15 20:00:39  ·  最后修改于 2026-05-16 8:26:18
已通过
审核员:Getaway_Car the cure · 2026-05-16 8:26:18

这道题是二分的板子题。
对于每个询问,我们可以用 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;
    }
}

暂无评论

登录 后即可评论。