백준/일반 문제

[백준/BOJ] 10815 - 숫자 카드 (c++) (이분탐색)

sem; 2022. 4. 8. 12:11
반응형

https://www.acmicpc.net/problem/10815

 

기본적인 이분탐색 문제입니다.

 


소스코드

 

#include <iostream>
#include <algorithm>

using namespace std;

int N, M, arrn[500000], a;

void Input() {
	cin >> N;
	for (int i = 0; i < N; i++) cin >> arrn[i];
	cin >> M;

	sort(arrn, arrn + N);
}

bool BS(int target) {
	int l = 0, r = N - 1, m;
	while (l <= r) {
		m = (l + r) / 2;
		if (arrn[m] == target) return true;
		else if (arrn[m] < target) l = m + 1;
		else r = m - 1;
	}
	return false;
}

void Solution() {
	cin >> a;
	if (BS(a)) cout << 1 << ' ';
	else cout << 0 << ' ';
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	Input();
	while (M--)
		Solution();
}
반응형