알고리즘 풀이/백준(Boj)

백준(BOJ) 10816번 숫자 카드 2

100win10 2019. 7. 23. 14:40

문제 : https://www.acmicpc.net/problem/10816


문제

숫자 카드는 정수 하나가 적혀져 있는 카드이다. 상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 몇 개 가지고 있는지 구하는 프로그램을 작성하시오.



나의풀이 :

가지고 있는 카드를 이진탐색을 통한 upper_bound 와 lower_bound를 이용하기 위해 오름차순 정렬해준다.

그 후 upper_bound에서 num에  lower_bound로 찾은 값을 빼주면 그 개수를 구할 수 있다.


#include <iostream>

#include <vector>

#include <algorithm>


using namespace std;


int main()

{

ios_base::sync_with_stdio(0);

cin.tie(0);

int N, M;

cin >> N;

vector<int> v(N, 0);

for (int i = 0; i < N; ++i)

cin >> v[i];

sort(v.begin(), v.end());

cin >> M;

for (int i = 0; i < M; ++i) {

int num;

cin >> num;


cout << upper_bound(v.begin(), v.end(), num) - lower_bound(v.begin(), v.end(), num) << " "; 

}

cout << endl;

return 0;

}