A - 二分查找(一)

A - 二分查找(一)

蒜头君手上有个长度为 nn 的数组 AA。由于数组实在太大了,所以蒜头君也不知道数组里面有什么数字,所以蒜头君会经常询问整数 xx 是否在数组 AA 中。

输入格式

第一行输入两个整数 nn 和 mm,分别表示数组的长度和查询的次数。

接下来一行有 nn 个整数 a_iai​。

接下来 mm 行,每行有 11 个整数 xx,表示蒜头君询问的整数。

输出格式

对于每次查询,如果可以找到,输出"YES",否则输出"NO"

数据范围

1 \le n, m \le 10^5, 0 \le x \le 10^61≤n,m≤105,0≤x≤106。

Sample 1

InputcopyOutputcopy
10 5
1 1 1 2 3 5 5 7 8 9
0
1
4
9
10
NO
YES
NO
YES
NO

#include<bits/stdc++.h>
using namespace std;
#define M 1000000
const int N=2e6+10;
int a[N],b[N],con=2e+10;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=m;++i){
        int x,l,r,mid;
        cin>>x;
        l=1,r=n;
        while(l<=r){
            mid=l+(r-l)/2;
            if(x==a[mid]) {cout<<"YES"<<endl;break;}
            else if(x<a[mid]) r=mid-1;
            else if(x>a[mid]) l=mid+1;
        }
        if(l>r) cout<<"NO"<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值