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
Inputcopy | Outputcopy |
---|---|
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;
}