728x90

 0. [c++] 백준  - 


https://www.acmicpc.net/problem/10815


 1. 풀이


이전에 백준 1920문제와 동일한 방식의 풀이를 활용하면 된다.

https://kyunstudio.tistory.com/165



 2. 소스코드


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<algorithm>
 
using namespace std;
 
int Arr[500001];
int Arr2[500001];
 
void binarySearch(const int & N, const int & K) {
    int first = 0;
    int last = N - 1;
    int mid = 1;
    while (first <= last) {
        mid = (first + last) / 2;
        //cout << mid << endl;
        if (Arr[mid] == K) {
            cout << "1\n";
            return;
        }
        else {
            if (Arr[mid] > K)
                last = mid - 1;
            else
                first = mid + 1;
        }
    }
    cout << "0\n";
    return;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int N, M;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> Arr[i];
    }
    cin >> M;
    for (int i = 0; i < M; i++) {
        cin >> Arr2[i];
    }
 
    sort(Arr, Arr + N);
    for (int i = 0; i < M; i++) {
        binarySearch(N, Arr2[i]);
    }
 
}
cs


 3. 참고


https://kyunstudio.tistory.com/165



질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


+ Recent posts