반응형
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();
}
반응형
'백준 > 일반 문제' 카테고리의 다른 글
[백준/BOJ] 1003 - 피보나치 함수(c++) (DP) (0) | 2022.04.08 |
---|---|
[백준/BOJ] 1149 - RGB거리 (c++) (DP) (0) | 2022.04.08 |
[백준/BOJ] 21609 - 상어 중학교 (c++) (0) | 2022.04.07 |
[백준/BOJ] 20057 - 마법사 상어와 토네이도 (c++) (0) | 2022.04.06 |
[백준/BOJ] 20056 - 마법사 상어와 파이어볼 (c++) (0) | 2022.04.06 |