As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
7 1 2 3 4 5 6 7
2 3 2 1 6 5 4
3 2 2 3
0
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#define bug(x) printf("**%d\n",x)
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
/*
优先队列的特点,就是可以动态的修改
最主要的思想还是从最多的开始找这种策略
我的思想重心放在了,找到底能够组成多少个,而忽略了,我们这种策略是不是最优的
比如这组数据 33 11 5555 4444 , 就不能这种一个从头开始选一个的策略
如果选择两个数的话,就没有什么关系了
*/
const int maxn=1e5+10;
struct node {
int x,num;
node() { }
node(int _x,int _num) {
x=_x,num=_num;
}
/*
这就是数据结构中的排序二叉树,
greater 小顶堆
less 大顶堆
priority_queue<node,vector<node>,less> q;//大顶堆
priority_queue<node,vector<node>,greator> q;//小顶堆
*/
bool operator<(const node& b)const {
return num<b.num;// greater小顶堆
}
};
int val[maxn],num[maxn];
int ans[maxn][3];
map<int,int> mp;
priority_queue<node> pq;
int main()
{
int dif=0;
int n,m;
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%d",&val[i]);
if(!mp[val[i]]) {
mp[val[i]]=1;
num[dif++]=val[i];
} else
mp[val[i]]++;
}
for(int i=0; i<dif; i++) {
pq.push(node(num[i],mp[num[i]]));
}
int now=0,j=0;
while(pq.size()>=3) {
node tmp1=pq.top();
pq.pop();
node tmp2=pq.top();
pq.pop();
node tmp3=pq.top();
pq.pop();
ans[now][0]=tmp1.x,tmp1.num--;
ans[now][1]=tmp2.x,tmp2.num--;
ans[now][2]=tmp3.x,tmp3.num--;
now++;
if(tmp1.num)
pq.push(tmp1);
if(tmp2.num)
pq.push(tmp2);
if(tmp3.num)
pq.push(tmp3);
}
printf("%d\n",now);
for(int i=0; i<now; i++) {
int a=ans[i][0],b=ans[i][1],c=ans[i][2];
if(a>b) swap(a,b);
if(a>c) swap(a,c);
if(b>c) swap(b,c);
printf("%d %d %d\n",c,b,a);
}
return 0;
}