1400*D. Candy Box (easy version)(贪心)
3
10
9
Example
input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
output
题意:
n个糖果,分为多个种类,要求尽可能的多选,并且使得不同种类的数量不能相同。
解析:
记录每种糖果的数量,然后排序,对于每种糖果尽可能的多拿,并且保证数量不同。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int q,n,a[N],t;
int main(){scanf("%d",&q);while(q--){scanf("%d",&n);map<int,int>mp; for(int i=0;i<n;i++){scanf("%d",&t);mp[t]++;}vector<int>a;for(auto it:mp){a.push_back(it.second);}sort(a.begin(),a.end());int res=0,f=2e5+1;for(int i=a.size()-1;i>=0;i--){if(a[i]==0||f<=0) break;if(a[i]>=f) res+=f,f-=1;else res+=a[i],f=a[i]-1;} printf("%d\n",res);}return 0;
}