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

github : https://github.com/junho0956/Algorithm/blob/master/10815/10815/%EC%86%8C%EC%8A%A4.cpp

 

숫자의 범위가 크기때문에 배열을 이용할 수는 없다.

그래서 탐색을 통해 존재여부를 찾아야하므로 가장 기본적인 binary search 를 아는지 모르는지를 묻는 문제이다.

 

더보기
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> v;
 
int search(int s, int e, int k) {
    int mid;
    while (s <= e) {
        mid = (s + e) / 2;
        if (v[mid] == k) return 1;
        else if (v[mid] > k) e = mid - 1;
        else if (v[mid] < k) s = mid + 1;
    }
    if (v[s] == k || v[e] == k) return 1;
    else return 0;
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int N, n, M, m;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> n;
        v.push_back(n);
    }
    sort(v.begin(), v.end());
    cin >> M;
    for (int i = 0; i < M; i++) {
        cin >> m;
        cout << search(0, N - 1, m) << ' ';
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 11662번 민호와 강호  (0) 2020.01.16
BOJ 10816번 숫자 카드2  (0) 2020.01.16
BOJ 2110번 공유기 설치  (0) 2020.01.16
BOJ 2805번 나무 자르기  (0) 2020.01.16
BOJ 1654번 랜선 자르기  (0) 2020.01.16

+ Recent posts