10816 - 숫자 카드 2

solution

code

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

bool comp(pair<int, int> a, pair<int, int> b)
{
    return a.second < b.second;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int N;
    cin >> N;
    int * arrN = new int[N];
    int elem;
    for(int i = 0; i < N; i++)
        cin >> arrN[i];
    sort(arrN, arrN + N);

    int M;
    cin >> M;
    pair<int, int> * arrM = new pair<int, int>[M];
    int * ans = new int[M];
    for(int i = 0; i < M; i++)
    {
        cin >> arrM[i].first;
        arrM[i].second = i;
    }
    sort(arrM, arrM + M);

    int first, last;
    for(int i = 0; i < M; i++)
    {
        first = lower_bound(arrN, arrN + N, arrM[i].first) - arrN;
        last = upper_bound(arrN, arrN + N, arrM[i].first) - arrN;
        arrM[i].first = last - first;
    }
    sort(arrM, arrM + M, comp);
    for(int i = 0; i < M; i++)
        cout << arrM[i].first << " ";
    cout << endl;

    return 0;
}

ref

10816번: 숫자 카드 2