思路:離散化,之後貪心選取最多的三個種類的物品(注意每次要一個個選,因爲要保證每個階段的選擇都是最優的,如果一下子拿,就可能導致某個階段的選擇不是最有選擇);
例:
16
1 1 2 2 3 3 4 4 4 4 5 5 5 5 6 6
ans=5;
代碼如下:
/*
*/
#define LOCAL
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define maxn 100300
#define inf 40000000
#define LL long long
map<int,int> M;
int main()
{
#ifdef LOCAL
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
#endif
int n,k;
cin>>n;
M.clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
if(M.find(k)==M.end()) M[k]=1;
else M[k]++;
}
priority_queue<int >q;
while(!q.empty())q.pop();
for(auto p:M)
{
q.push(p.second);
}
int ans=0;
while(!q.empty())
{
int k1=q.top();q.pop();
if(q.empty()) break;
int k2=q.top();q.pop();
if(q.empty()) break;
int k3=q.top();q.pop();
k1--;k2--;k3--;
ans++;
if(k1) q.push(k1);
if(k2) q.push(k2);
if(k3) q.push(k3);
}
printf("%d\n",ans);
return 0;
}