import java.util.Scanner;
public class Main{
public static int n,q;
public static int[] arr;
public static int[] qrr;
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
input(scan);
doSearch();
scan.close();
}
public static void doSearch()
{
for(int i=0;i<qrr.length;i++)
{
bSearch(qrr[i]);
}
}
public static void bSearch(int x)
{
int s = 0;
int e = arr.length-1;
if(x==arr[s] || x==arr[e]){System.out.println("YES");return;}
if(x<arr[s] || x>arr[e]){ System.out.println("NO");return;}
while(s+1<e)
{
int m = (s+e)/2;
if(arr[m]==x){System.out.println("YES");return;}
else if(arr[m]<x){s=m;}
else{e=m;}
}
System.out.println("NO");
}
public static void bSearchRecursive(int s,int e,int x)
{
if(s>e){System.out.println("NO");return;}
if(s==e)
{
if(arr[s]==x){System.out.println("YES");return;}
else{System.out.println("NO");return;}
}
int m = (s+e)/2;
if(arr[m]==x){System.out.println("YES");return;}
else if(arr[m]<x)
{bSearch(m+1,e,x);}
else
{bSearch(s,m-1,x);}
}
public static void input(Scanner scan)
{
n = scan.nextInt();
q = scan.nextInt();
arr = new int[n];
qrr = new int[q];
for(int i=0;i<n;i++)arr[i] = scan.nextInt();
for(int i=0;i<q;i++)qrr[i] = scan.nextInt();
}
}
오랜만에 다시 풀었는데, 헤맸음.
경계로 범위를 좁혀가냐, 값으로 범위를 좁혀가냐 이거 잘 따져야 함.
'알고리즘' 카테고리의 다른 글
보호필름 설계 & 구현 (0) | 2019.09.24 |
---|---|
등산로 조성 (0) | 2019.09.24 |
보호필름[모의문제] (0) | 2019.09.17 |
재귀 유용한 틀(1) (0) | 2019.09.16 |
수영장 (0) | 2019.09.04 |