Problem Description
You are given a sequence of positive integers of length N, a=(a1,a2,…,aN). Your objective is to remove some of the elements in a so that a will be a good sequence.
Here, an sequence b is a good sequence when the following condition holds true:
For each element x in b, the value x occurs exactly x times in b.
For example, (3,3,3), (4,2,4,1,4,2,4) and () (an empty sequence) are good sequences, while (3,3,3,3) and (2,4,1,4,2) are not.Find the minimum number of elements that needs to be removed so that a will be a good sequence.
Constraints
- 1≤N≤105
- ai is an integer.
- 1≤ai≤109
Input
Input is given from Standard Input in the following format:
N
a1 a2 … aNOutput
Print the minimum number of elements that needs to be removed so that a will be a good sequence.
Example
Sample Input 1
4
3 3 3 3Sample Output 1
1
We can, for example, remove one occurrence of 3. Then, (3,3,3) is a good sequence.Sample Input 2
5
2 4 1 4 2Sample Output 2
2
We can, for example, remove two occurrences of 4. Then, (2,1,2) is a good sequence.Sample Input 3
6
1 2 2 3 3 3Sample Output 3
0
Sample Input 4
1
1000000000Sample Output 4
1
Remove one occurrence of 109. Then, () is a good sequence.Sample Input 5
8
2 7 1 8 2 8 1 8Sample Output 5
5
题意:给出一个长度为 n 的序列,现在要在序列中删除一些数,使得删除后的序列中的每个元素 x 恰好出现 x,问要删除的最小元素数
思路:使用桶排统计计算即可,但由于序列元素最大能到 1E9,无法开这么大的数组,因此要使用 map 来进行统计
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 50+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
map<int,int> mp;
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
mp[x]++;
}
map<int,int>::iterator it;
int res=0;
for(it=mp.begin();it!=mp.end();it++){
int num=it->first;
int times=it->second;
if(num>times)
res+=times;
else
res+=(times-num);
}
printf("%d\n",res);
return 0;
}