C. New Year Snowmen

output
standard output

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
Copy
7
1 2 3 4 5 6 7
Output
2
3 2 1
6 5 4
Input
Copy
3
2 2 3
Output
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值