New Year Snowmen codeforces 140C

题目

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

 题目大意

 输入一个数字n,然后给出n个数字。

要求每三个不一样数字组合在一起输出。

输出时要输出最多的数字组合。

重点是 输出的时候,三个数字要按大小排列!!! (因为这个点,WA了好多次)

 算法

与其说是算法,不如说是方法:map,优先队列,贪心 

思路 

首先使用自定义结构体来存储半径和数量。

1 输入

输入的时候用map来存储,key用来表示半径,value来存储数量。

这样就可以统计有多少个重复的半径

2 排序 优先队列

由于map是按key排序,而我们需要的是按value排序

所以现在将map全部转移到结构体数组中

然后将结构体数组入队 实现按num排序

3 重点

贪心思想

首先优先队列中是按每一种半径的数量多少进行的排序

每次都取队列中的前三个(数量最多的三个)组成一组

当队列中元素少于三个时,结束

代码

#include <iostream>
#include <map>
#include <queue>
#include<algorithm>
using namespace std;
struct student
{
    int r;
    int num;
    	bool operator <(  student b)  const
    {    
		
	  		return num<b.num;  ///重载为 数量大优先
    }    

};

int cmp2(student a,student b)
{
	return a.r>b.r;			//升序
}
int show1[100005][3]; 

int main()
{
    map<int, int> M;
    int i,j,n;
    cin>>n;
    for(i=0;i<n;i++) 
	{
		cin>>j;
		M[j]++;
	}
	int len=M.size();
	student a[len];
	i=0;
	priority_queue<student> qq;
    for (map<int, int>::iterator it = M.begin(); it != M.end(); it++)
	{
		a[i].r=it->first;
		a[i].num=it->second;
		i++;
	}
	for(i=0;i<len;i++)
		qq.push(a[i]);
	int ans=0;
	while(qq.size()>=3)
	{
		student temp[3];
		temp[0]=qq.top();qq.pop();temp[0].num--;
		temp[1]=qq.top();qq.pop();temp[1].num--;
		temp[2]=qq.top();qq.pop();temp[2].num--;
		sort(temp,temp+3,cmp2);
		show1[ans][0]=temp[0].r;
		show1[ans][1]=temp[1].r;
		show1[ans][2]=temp[2].r;
		if(temp[0].num!=0) qq.push(temp[0]);
		if(temp[1].num!=0) qq.push(temp[1]);
		if(temp[2].num!=0) qq.push(temp[2]); 
		ans++;
	}
	cout<<ans<<endl;
	if(ans!=0)
		for(i=0;i<ans;i++)
			cout<<show1[i][0]<<" "<<show1[i][1]<<" "<<show1[i][2]<<endl;
		
	return 0;   
}

猜你喜欢

转载自blog.csdn.net/baidu_41907100/article/details/84707747