题解:AT_abc417_b Search and Delete

· · 题解

先开一个 map 存原数列中每个数出现的个数。对于每一个删数操作,判断数列中是否还有该数,如果有就将 map 中该数对应的值减一。最后遍历 map,同时输出即可。因为使用的是 map,所以最后的输出保证了是有序的。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int n, m;
map <int, int> cnt;

int main(){

    ios :: sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 1, x; i <= n; i++) cin >> x, cnt[x]++;
    for(int i = 1, x; i <= m; i++){
        cin >> x;
        if(cnt[x]) cnt[x]--;
    }
    auto it = cnt.begin();
    while(it != cnt.end()){
        for(int i = 1; i <= (*it).second; i++)
            cout << (*it).first << ' ';
        it++;
    }

    return 0;
}