为什么全RE

P2249 【深基13.例1】查找

wangjiahehe1118 @ 2024-05-25 14:33:55

#include <bits/stdc++.h>
using namespace std;
int a[100100];
int main () {
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    int k;
    for(int i=1;i<=m;i++){
        cin>>k;
        int r=1;
        int l=n;
        int ans;
        int mid=(r+l)/2;
        while(r<=l){
            mid=(r+l)/2;
            if(a[mid]>=k){
                l=mid-1;
                ans=mid;
            }else{
                r=mid+1;
            }
            mid=(r+l)/2;
        }
        if(a[ans]==k){
            cout<<ans<<" ";
        }else{
            cout<<"-1"<<" ";
        }
    }
    return 0;
}

by lpsz2024wcs @ 2024-05-25 14:42:36


by qazsedcrfvgyhnujijn @ 2024-05-25 14:59:50

建议lower_bound


|