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 r 1, r 2, …, r n. 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 r 1, r 2, …, r n (1 ≤ r i ≤ 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.
这一题除了注意元素的大小循序,还要按照重复元素多少进行排序,这样才能保证雪人数量最大。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define fors( x, y,z) for(int x=y;x<=z;x++)
#define test printf("<><><><><>\n");
int n;
int arr[1000001];
map<int,int>store;
struct cmp{
bool operator()(int x,int y)const {
if(store[x]==store[y])return x<y;
else return store[x]<store[y];}
};
priority_queue<int,vector<int>,cmp>bag;
void init()
{
int ttol;
scanf("%d",&n);
fors(i,1,n){
scanf("%d",&ttol);
store[ttol]++;
}
}
void solve()
{
int v=0;
map<int,int>::iterator it=store.begin();
for(it;it!=store.end();it++)
bag.push(it->first);
while(bag.size()>=3){
int x=bag.top();
store[x]--;
bag.pop();
int y=bag.top();
bag.pop();
store[y]--;
int z=bag.top();
bag.pop();
store[z]--;
int vl=arr[0]*3;
if(x<y) swap(x,y);
if(x<z) swap(x,z);
if(y<z) swap(y,z);
arr[vl+1]=x;
arr[vl+2]=y;
arr[vl+3]=z;
arr[0]++;
if(store[x])bag.push(x);
if(store[y])bag.push(y);
if(store[z])bag.push(z);
}
printf("%d\n",arr[0]);
fors(i,0,arr[0]-1)
printf("%d %d %d\n",arr[i*3+1],arr[i*3+2],arr[i*3+3]);
}
int main()
{
init();
solve();
}