Codeforces Round #100//140C - New Year Snowmen C. New Year Snowmen

New Year Snowmen
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
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 r1r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 12 and 3 can be used to make a snowman but 223 or 222 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 r1r2, ..., 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

先排序,然后扫一遍,找到每个不同的数出现的次数(可以用map实现),然后优先队列,每次出次数多的,直到空,注意每次出队后用一个,然后在入队(如果非零)

#include<stdio.h>
#include<algorithm>
#include<queue> 
#include<string.h>
using namespace std;
int num[100005];
struct node
{
	int shu,inde;
	bool operator <(const node &u)const
	{
		return shu<u.shu;
	}
}book;
struct node2
{
	int x,y,z;	
}ans[100005];
int cmp(node a ,node b)
{
	return a.shu>b.shu;
}
int main()
{
	int t;
	int n,i,j,re;
	priority_queue<node>q;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&num[i]);
	sort(num,num+n);
	book.shu=0;
	for(j=0,i=0;i<n-1;i++)
		{
			book.shu++;
			book.inde=num[i];
			if(num[i]!=num[i+1])
			{
				q.push(book);
				book.shu=0;
			}
		}
		if(num[n-1]==num[n-2])
		{
			book.shu++;
			q.push(book);
		}
		else
		{
			book.shu++;
			book.inde=num[n-1];
			q.push(book);
		}
	//	while(!q.empty())
	//	{
	//		book=q.top();
	//		q.pop();
	//		printf("%d %d\n",book.inde,book.shu);
	//	}
		j=0;
		node a,b,c;
		while(!q.empty())
		{
			a=q.top();
			q.pop();
			if(q.empty())
				break;
			b=q.top();
			q.pop();
			if(q.empty())
				break;
			c=q.top();
			q.pop();
			ans[j].x=a.inde;
			ans[j].y=b.inde;
			ans[j].z=c.inde;
			j++;
			a.shu--;
			b.shu--;
			c.shu--;
			if(a.shu>0)
				q.push(a);
			if(b.shu>0)
				q.push(b);
			if(c.shu>0)
				q.push(c);
		}
		printf("%d\n",j);
		int x1,x2,x3;
		for(i=0;i<j;i++)
		{
			if(ans[i].x>ans[i].y)
			{
				x1=ans[i].x;
				x2=ans[i].y;
			}
			else
			{
				x1=ans[i].y;
				x2=ans[i].x;
			}
			if(ans[i].z>x1)
			{
				printf("%d %d %d\n",ans[i].z,x1,x2);
			}
			else if(ans[i].z<x2)
			{
				printf("%d %d %d\n",x1,x2,ans[i].z);
			}
			else
			{
				printf("%d %d %d\n",x1,ans[i].z,x2);
			}
		}
		return 0;
}
	





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值