求助!

P2249 【深基13.例1】查找

zyc1219 @ 2023-03-21 13:25:54

#include<bits/stdc++.h>
using namespace std;
int a[1000005],x,n,m,l,mid,r,h;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=m;i++){
        scanf("%d",&x);
        l=1,r=n,h=0;
        while(l<=r){
            mid=(l+r)/2;
            if(a[mid]==x){
                for(h=mid;;h--)if(a[h]!=a[h-1])break;
                printf("%d ",h);
                break;
            }else if(x>a[mid])l=mid+1;
            else r=mid-1;
        }if(l>r)printf("-1 ");
    }return 0;
}

|