E - New Year Snowmen

 题目链接:https://cn.vjudge.net/contest/272792#problem/E

E - New Year Snowmen

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.

Input

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.

Output

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.

Examples

Input

7
1 2 3 4 5 6 7

Output

2
3 2 1
6 5 4

Input

3
2 2 3

Output

0

思路:

贪心+优先队列,先把所有的r和num打包装进node结点,然后按照num的大小进队,出队三个元素,num--,不断地进队出队。都是num大的先出队,记个ok记录总个数。

知识点:


priority_queue<node> qq;使用默认优先队列 ,按照j降序 ,需要重载运算符< , 详解:https://blog.csdn.net/c20182030/article/details/70757660

sort(a,b)//排序函数 

int cmp ()
return a>b;//按照降序,(a<b)按照升序。 如果不定义cmp,默认从小到大,升序

运算符重载

bool operator <(  node b)  const
    {    
        
              return num<b.num;  ///重载为 数量大优先
    }

这个重载可以按照结点的数量排序,降序

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
int ans[100000+5][3]; 
int vis[100000+5]; 
struct node
{
	int num,x;
 
	bool operator <(  node b)  const
    {    
		
	  		return num<b.num;  ///重载为 数量大优先 降序
    }    
};
priority_queue<node> qq;//使用默认优先级比较符号<
int cmp(int a ,int b)
{
	return a>b;			//降序
}
int cmp2(node a ,node b)
{
	return a.x>b.x;			//降序
}
 

int main()
{
	int n;
	cin>>n;
	int t;
	int i;
	for (i=1;i<=n;i++)
	{
		scanf("%d",&t);
		vis[i]=t;
	}
	sort(vis+1,vis+1+n,cmp);
 
	
	for(i=1;i<=n;i++)
	{
		int cun=1;
		while(vis[i]==vis[i+1]&&i<=n)
		{
			cun++;
			i++;
		}	
		node t;
		t.num=cun;//
		t.x=vis[i];
		qq.push(t);//得到重复的个数及该数值 进入优先队列 
	}
  
	int ok=0;
	while(qq.size()>=3)     //不同的雪球种类>=3
	{
		node q[4];
		q[1].x=q[2].x=q[3].x=0;      
			q[1]=qq.top(),qq.pop(); //取出前三大,复杂度logn
			q[2]=qq.top(),qq.pop(); 
			q[3]=qq.top(),qq.pop();
sort(q+1,1+q+3,cmp2);			//把雪球的半径排序,升序
	 
			ans[++ok][0]=q[1].x;	//记录答案
			ans[ok][1]=q[2].x;
			ans[ok][2]=q[3].x;
 
 
		q[1].num--;q[2].num--;q[3].num--;    //数量减一
 
		if(q[1].num)					//若个数为零,丢弃,否则继续进入队列
			qq.push(q[1]);
		if (q[2].num)
			qq.push(q[2]);
		if (q[3].num)
			qq.push(q[3]);
		
	}
	printf("%d\n",ok);
	for (i=1;i<=ok;i++)
	{
		printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
	}


	return 0;
}

注:个别代码选自网络,侵权请私信联系删除。

本人初学者,欢迎大家一起敲代码呀 呀咿呀咿呀~~~~~~~~

猜你喜欢

转载自blog.csdn.net/weixin_43442778/article/details/84704684