Good sequence(AtCoder-3725)

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 … aN

Output

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 3

Sample 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 2

Sample 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 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1
Remove one occurrence of 109. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample 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;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值