Little Jay

[C++] 백준 1920 수 찾기 본문

알고리즘/BOJ

[C++] 백준 1920 수 찾기

Jay, Lee 2021. 7. 18. 16:25

처음에 시간 초과가 떠서 봤더니 

ios_base::sync_with_stdio(0); 
cin.tie(0);

이거 두개 안해서 틀렸었다.

항상 시간 초과가 나면 킹받는다.

 

이분(이진)탐색 즉, Binary Search를 이용하는 문제이다

자료구조를 배우긴 했지만 Binary Search를 배우지는 않아서

algorithm 헤더에 있는 binary_search 메소드를 사용했다

이 부분 구현은 혼자 따로 해봐야 겠다

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {

	ios_base::sync_with_stdio(0); 
	cin.tie(0);

	int n, m, x;
	cin >> n;
	vector<int> v;
	for (int i = 0; i < n; i++) {
		cin >> x;
		v.push_back(x);
	}
	cin >> m;
	sort(arr, arr + n);
	sort(v.begin(), v.end());
	for (int i = 0; i < m; i++) {
		cin >> x;
		if (binary_search(v.begin(), v.end(), x))
			cout << 1 << "\n";
		else
			cout << 0 << "\n";
	}
	return 0;
}

 

'알고리즘 > BOJ' 카테고리의 다른 글

[C++] 백준 2749번 피보나치 수 3  (0) 2021.07.23
[C++] 백준 9471번 피사노 주기  (0) 2021.07.23
[C++] 백준 15829번 Hashing  (0) 2021.07.18
[C++] 백준 10816번 숫자 카드 2  (0) 2021.07.18
[C++] 백준 5054번 주차의 신  (0) 2021.07.16
Comments