[BOJ][실4][10815] 숫자 카드

문제 링크

문제링크

첫 번째 풀이 : Greedy

알고리즘

lower_bound, upper_bound 모두 값을 찾지 못하면 last()에 대한 pointer를 return 합니다.

즉, 배열에 값이 없다면 lower_bound, upper_bound의 return 값이 같습니다.

정답코드

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
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int MAX = 101;

int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };

int n, m;
vector<int> v;

int main()
{
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    int a;
    for (int i = 0; i < n; i++) {
        cin >> a;
        v.push_back(a);
    }
    sort(v.begin(), v.end());

    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> a;
        auto left = lower_bound(v.begin(), v.end(), a);
        auto right = upper_bound(v.begin(), v.end(), a);
        if (left < right)cout << "1 ";
        else cout << "0 ";
    }
    cout << "\n";
    return 0;
}

Success Notice: 수고하셨습니다. :+1:

Leave a comment