https://www.acmicpc.net/problem/1920
1920번: 수 찾기
첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들
www.acmicpc.net
1. 이분탐색 stl
binary_search(begin, end , target) 넣으면
target이
있으면1, 없으면0 리턴해줌
2. 전체코드
#include <bits/stdc++.h>
using namespace std;
int n,m;
vector<int> v;
int main() {
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
int temp;
cin >> temp;
v.push_back(temp);
}
sort(v.begin(), v.end());
cin >> m;
for (int i = 0; i < m; ++i) {
int target;
cin >> target;
cout << binary_search(v.begin(), v.end(), target)<<"\n";
}
return 0;
}
'Algorithm > 이분탐색' 카테고리의 다른 글
프로그래머스 순위검색 c++ // 이분탐색, db설정 (0) | 2024.01.11 |
---|---|
백준 1654 랜선자르기 c++ // 이분탐색, parametric search (0) | 2024.01.04 |
백준 2295 세수의합 c++ // 이분탐색 발상 (0) | 2024.01.03 |
백준 18870 좌표압축 c++ // 이분탐색 (0) | 2024.01.02 |
백준 10816 숫자카드2 c++ // 이분탐색, upper_bound, lower_bound (0) | 2024.01.02 |