东北的冬季,尤其是过年的时候,小陈老师喜欢去堆雪人。
每个雪人主要由三个雪球构成:大雪球、中雪球、小雪球。
他已经准备好了N个雪球,半径分别等于r1, r2, …, rn。如果要堆一个雪人,就需要三个半径互不相等的雪球。
例如:
三个雪球的半径为1、2、3,能够用来堆一个雪人。但是半径为2、2、3或者2、2、2的三个雪球就不可以。
快帮帮小陈老师,算算他最多能用这些雪球堆多少个雪人。
Input
对于每组测试数据:
第1行,包含一个整数n(1≤n≤100000) — 雪球的数量。
第2行,包含n个整数 — 雪球的半径r1, r2, …, rn (1≤ri≤1000000000)。
处理到文件结束
Output
对于每组测试数据:
第1行,输出最多能堆多少雪人 - k。
接下来k行,每行描述一个雪人,每行用空格分割三个数字分别表示大雪球、中雪球、小雪球的半径。
可以用任何顺序输出每个雪人。如果有多种可行解,输出任意一个即可。
Sample Input
7
1 2 3 4 5 6 7
3
2 2 3
Sample Output
2
3 2 1
6 5 4
0
思路:每次都取数量最大的前三个,所以优先队列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#define ll long long
using namespace std;
const int M=365*24*60*60;
const int N=1e5+5;
struct node
{
int x,y;
bool operator<(const node &t)const
{
return y<t.y;
}
};
int n,x,b[N][3];
int main()
{
while(cin >> n)
{
map<int,int> m;
while(n--)
{
cin >> x;
m[x]++;
}
priority_queue<node> q;
for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
{
q.push({it->first,it->second});
}
int res=0,c[3];
while(!q.empty())
{
node t1=q.top();
q.pop();
if(q.empty())
break;
node t2=q.top();
q.pop();
if(q.empty())
break;
node t3=q.top();
q.pop();
c[0]=t1.x,c[1]=t2.x,c[2]=t3.x;
sort(c,c+3);
for(int i=2;i>=0;i--)
b[res][2-i]=c[i];
res++;
if(--t1.y)
q.push(t1);
if(--t2.y)
q.push(t2);
if(--t3.y)
q.push(t3);
}
printf("%d\n",res);
for(int i=0;i<res;i++)
{
for(int j=0;j<3;j++)
printf("%d ",b[i][j]);
printf("\n");
}
}
return 0;
}