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.
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.
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.
7 1 2 3 4 5 6 7
2 3 2 1 6 5 4
3 2 2 3
0
贪心的思想,从前三组(半径相同为一组)数量最多的雪球中各取一个构成一组答案,这样就保证多的尽可能被消耗
其次,不要被要求输出大中小三种型号的雪球迷惑,只要选出三个半径不同的就存在大中小这种排列
最后就是优先队列和映射的应用
#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
#define maxn 100005
using namespace std;
class list
{
public:
int num;
int amount;
friend bool operator <(list a,list b)
{
return a.amount<b.amount;
}
list() { }
list(int c,int d):num(c),amount(d) { }
};
int an[maxn],bn[maxn],cn[maxn];
int main()
{
int n;
while(cin >> n)
{
int cnt=0;
priority_queue<list> q;
map<int,int> mp;
while(n--)
{
int a;
cin >> a;
mp[a]++;
}
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
q.push(list(it->first,it->second));
}
while(q.size()>=3)
{
list a,b,c;
a=q.top();
q.pop();
b=q.top();
q.pop();
c=q.top();
q.pop();
an[cnt]=a.num,bn[cnt]=b.num,cn[cnt++]=c.num;
if(a.amount-1)
q.push(list(a.num,a.amount-1));
if(b.amount-1)
q.push(list(b.num,b.amount-1));
if(c.amount-1)
q.push(list(c.num,c.amount-1));
}
cout << cnt << endl;
for(int i=0;i<cnt;i++)
{
if(an[i]<bn[i])
swap(an[i],bn[i]);
if(an[i]<cn[i])
swap(an[i],cn[i]);
if(bn[i]<cn[i])
swap(bn[i],cn[i]);
}
for(int i=0;i<cnt;i++)
{
cout << an[i] << ' ' << bn[i] << ' ' << cn[i] << endl;
}
}
return 0;
}