post image post image post image post image post image post image post image post image

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

 

N이 최대 10만, M이 최대 10만이므로 N^2 풀이는 시간 초과가 발생한다. N개의 원소를 가진 배열을 받아 정렬 후 이분탐색으로 풀면된다.

 

#include <iostream>
#include <algorithm>

using namespace std;

int arr[100'000];

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n, m, tmp; 
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> arr[i];

	sort(arr, arr + n);

	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> tmp;
		cout << binary_search(arr, arr + n, tmp) << '\n';
	}
	return 0;
}
메모리: 2412 kb 시간: 60 ms

'컴퓨터 사이언스 > 1고리즘' 카테고리의 다른 글

백준 3300 : 무어 기계  (0) 2021.08.02
백준 1929 : 소수 구하기  (0) 2021.08.01
백준 1874 : 스택 수열  (0) 2021.08.01
백준 13460 : 구슬 탈출 2  (0) 2021.08.01
백준 13459 : 구슬 탈출  (0) 2021.08.01

+ Recent posts