๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 10815๋ฒˆ - ์ˆซ์ž์นด๋“œ

by dkswnkk 2021. 10. 24.
 

10815๋ฒˆ: ์ˆซ์ž ์นด๋“œ

์ฒซ์งธ ์ค„์— ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 500,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ˆซ์ž ์นด๋“œ์— ์ ํ˜€์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ˆซ์ž ์นด๋“œ์— ์ ํ˜€์žˆ๋Š” ์ˆ˜๋Š” -10,000,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10,

www.acmicpc.net

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int N, M;
    cin >> N;
    vector<int>v;
    for (int i = 0; i < N; i++) {
        int number; cin >> number;
        v.push_back(number);
    }
    sort(v.begin(), v.end());
    cin >> M;

    for (int i = 0; i < M; i++) {
        int start = 0;
        int end = N - 1;
        int check;
        cin >> check;
        while (start <= end) {
            int mid = (start + end) / 2;
            if (v[mid] == check) {        //์ฐพ๋Š”๊ฐ’์ด ์žˆ์œผ๋ฉด ์ถœ๋ ฅ
                cout << 1 << " ";
                break;
            }
            else if (v[mid] < check) {        //์ฐพ๋Š”๊ฐ’์ด ๋” ํฌ๋ฉด ์˜ค๋ฅธ์ชฝํƒ์ƒ‰
                start = mid + 1;
            }
            else {                        //์ฐพ๋Š” ๊ฐ’์ด ์ž‘์œผ๋ฉด ์™ผ์ชฝ ํƒ์ƒ‰
                end = mid - 1;
            }
            if (start > end) cout << 0 << " ";
        }
    }
}

๋Œ“๊ธ€