题意:
给出n个数字,要求找出不同的三个数字构成雪人,问最多可以构成多少组这种符合条件的要求
思路:
优先队列,排序为数字个数
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
int a[100005][5];
struct node
{
int num,sum;
friend bool operator <(node n1,node n2)
{
return n1.sum<n2.sum;
}
}s[100005];
int b[100005];
int main()
{
int n,t;
cin>>n;
priority_queue<node > q;
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
sort(b+1,b+1+n);
node c;
int x,y;
c.num=b[1],c.sum=1;
for(int i=2;i<=1+n;i++)
{
if(b[i]!=c.num )
{
q.push(c);
c.num=b[i];
c.sum=1;
}
else
{
c.sum++;
}
}
int ans=0;
int cnt=0;
while(!q.empty())
{
node first=q.top();
q.pop();
node second,third;
second.num=-1,third.num=-1;
if(!q.empty() )
{
second=q.top();
q.pop();
}
if(!q.empty() )
{
third=q.top();
q.pop();
}
if(second.num!=-1&&third.num!=-1)
{
a[++cnt][3]=third.num;
a[cnt][2]=second.num;
a[cnt][1]=first.num;
}
else
break;
first.sum--;
second.sum--;
third.sum--;
if(first.sum!=0)
{
q.push(first);
}
if(second.sum!=0)
{
q.push(second);
}
if(third.sum!=0)
{
q.push(third);
}
}
cout<<cnt<<endl;
for(int i=1;i<=cnt;i++ )
{
int max1=max(max(a[i][1],a[i][2]),a[i][3]);
int max2=min(min(a[i][1],a[i][2]),a[i][3]);
int max3;
for(int j=1;j<=3;j++)
{
max3=a[i][j];
if(max3!=max1&&max3!=max2)
break;
}
cout<<max1<<" "<<max3<<" "<<max2<<endl;
}
return 0;
}
/*
8
3 4 5 3 2 3 5
*/