[백준] 10815번 숫자 카드
https://www.acmicpc.net/problem/10815
10815번: 숫자 카드
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,000,000보다 작거나 같다. 두 숫자 카드에 같은 수가 적혀있는 경우는 없다. 셋째 줄에는 M(1 ≤ M ≤ 500,000)이 주어진다. 넷째 줄에는 상근이가 가지고 있는 숫자 카드인지 아닌지를 구해야 할 M개의 정수가 주어지며, 이
www.acmicpc.net
정렬을 해놓은 후 이진탐색(이분검색)을 이용하면 된다.
그리고 많은 수를 입력받고 많은 수를 출력해야 하기 때문에 입출력 시간을 줄이는 다음 코드를 써줘야 한다.
ios_base::sync_with_stdio(false);
cin.tie(NULL);
정렬하는 것은 <algorithm> 헤더의 sort() 함수를 이용하고 이진탐색은 binary_search() 함수를 이용하면 된다.
이 함수들의 사용법을 자세히 알고 싶다면 아래 링크에서 볼 수 있다.
https://breakcoding.tistory.com/117
[C++] vector (벡터) 정렬, 배열 정렬하기
정렬을 하려면 라이브러리의 sort() 함수를 쓰면 된다. 따라서 헤더파일을 포함해줘야 한다. sort() 함수의 첫번째 두번째 매개변수는 iterator, 즉 포인터이다. sort - C++ Reference cu..
breakcoding.tistory.com
https://breakcoding.tistory.com/188
[C++] 이진탐색 binary_search, upper_bound, lower_bound 함수 사용법
C++로 코딩을 하다가 이진탐색을 할 필요가 있다면 직접 구현할 필요없이 헤더에 정의되어 있는 binary_search() 함수를 사용하면 된다. C++에서 이진탐색을 어떻게 하는지 알아보자. 일단 이진탐색이..
breakcoding.tistory.com
코드는 다음과 같다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < v.size(); i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
int m;
cin >> m;
vector<bool> result(m);
for (int i = 0; i < m; i++) {
int temp;
cin >> temp;
result[i] = binary_search(v.begin(), v.end(), temp);
}
for (int i = 0; i < m; i++) {
cout << result[i] << " ";
}
return 0;
}