01-09 04:37
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ์ด๋ถ„ํƒ์ƒ‰ 1920 :: ์ˆ˜ ์ฐพ๊ธฐ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ์ด๋ถ„ํƒ์ƒ‰ 1920 :: ์ˆ˜ ์ฐพ๊ธฐ

miinsun 2022. 4. 20. 01:03

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], …, A[N]์ด ์ฃผ์–ด์ ธ ์žˆ์„ ๋•Œ, ์ด ์•ˆ์— X๋ผ๋Š” ์ •์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‹ค์Œ ์ค„์—๋Š” N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], …, A[N]์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‹ค์Œ ์ค„์—๋Š” M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‹ค์Œ ์ค„์—๋Š” M๊ฐœ์˜ ์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด ์ˆ˜๋“ค์ด A์•ˆ์— ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋ฉด ๋œ๋‹ค.
  • ๋ชจ๋“  ์ •์ˆ˜์˜ ๋ฒ”์œ„๋Š” -231 ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  231๋ณด๋‹ค ์ž‘๋‹ค.

 

์ถœ๋ ฅ

  • M๊ฐœ์˜ ์ค„์— ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์กด์žฌํ•˜๋ฉด 1์„, ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

5
4 1 5 2 3
5
1 3 7 9 5

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

1
1
0
0
1

 

โ€‹

๐Ÿ’ป  Main.java

/* ๋ฐฑ์ค€ ์ด๋ถ„ํƒ์ƒ‰ - 1920 :: ์ˆ˜ ์ฐพ๊ธฐ */
import java.io.*;
import java.util.*;

public class Main {	
	static int[] arr1;

	public static boolean isIn(int target) {
		// lt, rt ์ดˆ๊ธฐํ™”
		int lt = 0;
		int rt = arr1.length - 1;
		
		// lt, rt๊ฐ€ ๊ฐ™์•„์ง€๋ฉด ์ข…๋ฃŒ
		while(lt <= rt) {
			int mid = (lt + rt) / 2;
			if(arr1[mid] == target)
				return true;
			
			if(arr1[mid] < target) {	// ์ฐพ์•„์•ผํ•  ๊ฐ’์ด mid๊ฐ’๋ณด๋‹ค ํฌ๋ฉด, lt๋ฅผ ์ฆ๊ฐ€ํ•œ๋‹ค.
				lt = mid + 1;
			}
			else {
				rt = mid - 1;	// ์ฐพ์•„์•ผํ•  ๊ฐ’์ด mid๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด, rt๋ฅผ ๊ฐ์†Œํ•œ๋‹ค
			}
		}
		
		return false;
	}
	
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		// ๋ฐฐ์—ด ์ž…๋ ฅ
		int n = Integer.parseInt(br.readLine());
		StringTokenizer st = new StringTokenizer(br.readLine());
		arr1 = new int[n];
		for(int i = 0; i < n; i++)
			arr1[i] = Integer.parseInt(st.nextToken());
		
		// ๋ฐฐ์—ด ์ •๋ ฌ
		Arrays.sort(arr1);
		
		int m = Integer.parseInt(br.readLine());	
		st = new StringTokenizer(br.readLine());
		// ์ž…๋ ฅ ๊ฐ’ ์ฐพ๊ธฐ
		for(int i = 0; i < m; i++) {
			if(isIn(Integer.parseInt(st.nextToken())))	// ์ž…๋ ฅ ๊ฐ’์ด ๋ฐฐ์—ด์— ์žˆ์œผ๋ฉด
				sb.append(1).append('\n');
			else	// ์—†์œผ๋ฉด
				sb.append(0).append('\n');
		}
		
		System.out.println(sb);
		
		br.close();
	}
}
Comments