代码如下:
//使用set(不允许重复元素存在相当于map的key)和multiset(允许多个/重复元素存在相当于map的value)
//set用于记录出现了哪些数字;utilset用于装入所有的数字,最后用count计数;
//关于插入补充:map使用insert(插入一对),set使用insert(插入单个)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+9;
int a[N];
int main()
{
int n;cin>>n;
set<int> myset;
multiset<int> allset;
int deletenumber=0;//用于记录删除的数字个数
for(int i=0;i<n;++i)
{
cin>>a[i];
myset.insert(a[i]);//全部遍历输入,set会自动消去重复数字
allset.insert(a[i]);//全部遍历输入,mutilset会全部装入
}
for(auto x:myset)//使用自动变量遍历set集合
{
int num=allset.count(x);//相当于记录key为x时,对应的value中数字的数量
if(x>num)//如果该数本身大于该数存在的数量
deletenumber+=num;
else if(x<num)//如果该数本身小于该数的数量
deletenumber+=num-x;
}
cout<<deletenumber<<endl;
return 0;
}