BOJ. 10816

Opusdeisong·2022년 12월 27일
0

백준 Class2

목록 보기
28/31


#BOJ10816

숫자 카드 2

이 문제는 이분 탐색을 이용해야 하는데 이분 탐색을 여러번 하는 것이 아닌 Upper bound와 Lower bound간의 차를 통해서 몇 개가 나오는지를 확인해 주면 코드의 시간초과 문제를 해결하는데 도움이 많이 된다. 헤더는 역시 신이다. 이미 구현해놓으신 것들 잘 사용합니다^^

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    long long* arr = new long long[N];
    for (int i = 0; i < N; i ++){
        cin >> arr[i];
    }
    sort(arr, arr + N);
    int M;
    cin >> M;
    for (int i = 0; i < M; i ++){
        long long temp;
        cin >> temp;
        cout << upper_bound(arr, arr + N, temp) - lower_bound(arr, arr + N, temp) << " ";
    }
}
profile
Dorsum curvatum facit informaticum.

0개의 댓글