计蒜客 T1560 二分查找(一)

题目链接:https://nanti.jisuanke.com/t/T1560
算法特工队QQ羣:979618872 (伸手党绕边,欢迎有良好基础的人加入)

//
// Created by Leo Lee on 2019/4/5.
//

#include <iostream>
#include <algorithm>
using namespace std;
long long arr[1000000];
bool findInArr(long long n,long long longth);
int main(){
    long long count,times;
    scanf("%lld%lld",&count,&times);
    for(int i = 0;i<count;i++)
        scanf("%lld",&arr[i]);
    sort(arr,arr+count);
    long long tmp;
    while(times--){
        scanf("%lld",&tmp);
        if(findInArr(tmp,count))
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

bool findInArr(long long n,long long longth){
    long long L = 0,R = longth-1,mid;
    while(L<=R){
        mid = (R+L)/2;
        if (arr[mid] == n)
            return true;
        else if(arr[mid]<n)
            L = mid+1;
        else
            R = mid-1;
    }
    return false;
}
点赞