1. 程式人生 > >New Year Snowmen codeforces 140C

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, r

2, ..., 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;   
}