문제링크 : https://www.acmicpc.net/problem/10815



아이디어

  • 포함하고있는지 확인 >> 이분탐색

시간복잡도 계산

  • N개에 대해 정렬 : O(NlgN)
  • M개에 대해 이분 탐색 : O(M * lgK )
  • 합 : O(NlgN + MlgK)

인트 계산

  • 각 숫자의 최소 -1e7, 최대 1e7 > INT 가능

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
 
using namespace std;
 
int nums[500010];
 
 
void binsearch(int st, int en, int tar) {
    if(st==en) {
        if(nums[st] == tar) cout << 1 << ' ';
        else cout << 0 << ' ';
        return;
    }
    
    int mid = (st+en)/2;
    
    if(nums[mid] < tar) binsearch(mid+1, en, tar);
    else binsearch(st, mid, tar);
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int N; cin >> N;
    for(int i=0; i<N; i++cin >> nums[i];
    
    sort(nums, nums+N);
    
    int M; cin >> M;
    for(int i=0; i<M; i++){
        int a; cin >> a;
        binsearch(0,N-1,a);
    }
}
 
cs

문제유형

  • 이분탐색 기본

+ Recent posts