二分查找,也全WA了,大佬们给看看

P2249 【深基13.例1】查找

Do_www @ 2023-02-01 18:32:56


#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int q[N];
int  n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 0;i <= n;i ++) scanf("%d",&q[i]); 
    while(m--)
    {
        int x;
        cin>> x;
        int l = 1,r =n;
       while( l < r){
        int mid = l + r >> 1;
        if(q[mid] >= x) r = mid ;
        else l = mid + 1 ;
        }
        if(q[l] != x) cout<<"-1"<<endl;
        else cout<< l <<endl;

    }
    return 0;
}

by kugua @ 2023-02-09 13:43:49


|