试水2习题H---解题报告

题目叙述:

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

这个题用到的思想是贪心,为了记录所有的雪球的种类以及个数,用到了map,以映射的方法可以得到多大的雪球对应有多少个,然后采用优先队列,将连续三个雪球取出,如果个数还存在那么再返回队列,这里就需要一个结构体来存储雪球的大小与个数了,非常简单

AC code:

#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
const int maxn=1e5+5;
using namespace std;
map<int,int> m;
map<int,int>::iterator mm;
struct X

    int k,v;                                                             //k代表雪球的数量,v代表雪球的大小
    bool operator <(const X &r)const {
        return k<r.k;}                                                           //因为是优先队列,需要一个排序方式
};
priority_queue<X> q;
int a[maxn],b[maxn],c[maxn],n,g,h;
int main()
{
    cin>>n;
for(int i=0;i<n;i++)
    {
        cin>>g;
        m[g]++;                              //雪球g的个数+1
    }
    for(mm=m.begin();mm!=m.end();mm++)
    {X p;
    p.k=mm->second;
    p.v=mm->first;
        q.push(p);                                    //求不同大小雪球和个数一起放入优先队列当中
    }
    if(q.size()<3) {cout<<0<<endl;return 0;}         //如果q中元素少于3,说明组不成雪人,直接停止程序
    h=0;
    while(q.size()>=3)
    { 
        X x=q.top();q.pop();                //取出三个元素
        X y=q.top();q.pop();
        X z=q.top();q.pop();
        a[h]=x.v;b[h]=y.v;c[h]=z.v;h++;
        x.k--;y.k--;z.k--;              //数量减一
        if(x.k) q.push(x);
        if(y.k) q.push(y);                //这三个方程表示如果这三个大小的雪球还有个数的话返回队列,进一步操作
        if(z.k) q.push(z);
    }
    cout<<h<<endl;                              //h即为组成雪人的数量
    for(int i=0;i<h;i++)
        {
            if(a[i]<b[i]) swap(a[i],b[i]);                //这里是对三个数的排序,保证a>b>c的方式输出
            if(a[i]<c[i]) swap(a[i],c[i]);
            if(b[i]<c[i]) swap(b[i],c[i]);
            cout<<a[i]<<" "<<b[i]<<" "<<c[i]<<endl;
         }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值