10816

윤재학·2022년 8월 7일

백준

목록 보기
8/8
  • cin의 속도를 빠르게 하기 위해서 ios_base::sync_with_stdio(false); cin.tie(0);
    를 추가해주고 다음을 사용해 풀이.

  • upper_bound: 찾고 싶은 값을 초과하는 값이 처음 나오는 인덱스 (<)

  • lower_bound: 찾고 싶은 값 이상의 값이 처음 나오는 인덱스 (<=)

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    vector<int> v;
    int n, m;
 
    cin >> n;
    int num;
    for (int i = 0; i < n; i++)
    {
        cin >> num;
        v.push_back(num);
    }
 
    sort(v.begin(), v.end());
 
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> num;
        cout << upper_bound(v.begin(), v.end(), num) - lower_bound(v.begin(), v.end(), num) << ' ';
    }
 
    return 0;
}
profile
노력하자 즐겁게 개발할수 있는 환경을 위해

0개의 댓글