毕业生的纪念礼物
题目描述
现在有n个纪念品,每个纪念品都有一个种类r[i],现在要求对每个毕业生分发三个种类不同的纪念品,现在需要你来计算下共可以发给多少个毕业生?
输入
第一行一个整数n,1≤n≤100000,代表纪念品的个数;
第二行包含n个整数,分别是r[1], r[2], r[3]......r[n],1≤r[i]≤1e9,表示每个纪念品所属的种类。
第二行包含n个整数,分别是r[1], r[2], r[3]......r[n],1≤r[i]≤1e9,表示每个纪念品所属的种类。
输出
输出一个整数,代表最多能够分发给的毕业生人数。
样例输入
样例一
7
1 2 3 4 5 6 7
样例二
14
1 1 2 2 3 3 4 4 4 4 5 5 5 5
样例输出
样例一
2
样例二
4
提示
对于样例二,一种合理的分配方式是:
1 4 5
2 4 5
3 4 5
1 2 3
当然分配方式并不唯一。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int num[maxn];
struct node
{
int cnt;
int cur;
node(int x=0,int y=0)
{
cur=x;
cnt=y;
}
bool operator<(const node&other)const
{
return cnt<other.cnt;
}
};
priority_queue<node> pq;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1);
int now=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[i-1])
{
now++;
}
num[now]++;
}
for(int i=1;i<=now;i++)
{
pq.push(node(i,num[i]));
}
int ans=0;
while(pq.size()>=3)
{
ans++;
node x1=pq.top();
pq.pop();
node x2=pq.top();
pq.pop();
node x3=pq.top();
pq.pop();
x1.cnt--;
x2.cnt--;
x3.cnt--;
if(x1.cnt>0) pq.push(x1);
if(x2.cnt>0) pq.push(x2);
if(x3.cnt>0) pq.push(x3);
}
printf("%d\n",ans);
return 0;
}