求助,64

P2249 【深基13.例1】查找

Miracle1024 @ 2023-05-07 15:54:38

闰土

记录

#include<iostream>
using namespace std;
int a[1001001]={0};
int n,m,s,k;
int fnd(int s){
    int l=1,r=n,mid=0;
    while(l<r){
        mid=l+((r-l)>>1);
        if(a[mid]<=s) l=mid+1;
        else r=mid;
    }
    if(a[l-1]==s) return l-1;
    else return -1;
}
int main(){
//  freopen("doc.in","r",stdin);
//  freopen("doc.out","w",stdout);
    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",&s);
        k=fnd(s);
        while(a[k]==a[k-1]&&k>0){
            k--;
        }
        printf("%d ",k);
    }
    return 0;
}

|