New Year Snowmen(CF-140C)

Problem Description

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 最大到 1E9,没法开这么大的数组,因此要用 map 存储,此外,由于要保证每次取的都是个数最多的三个,取完后个数要减一,因此使用优先队列按个数排序

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

struct Node {
    int num;
    int now;
    Node(){}
    Node(int num,int now):num(num),now(now){}
    bool operator <(const Node &rhs)const {
        if(num==rhs.num)
            return now<rhs.now;
        return num<rhs.num;
    }
}node[N];
struct Res {
    int a;
    int b;
    int c;
}res[N];
map<int,int> mp;
int r[N];

void Sort(int &a,int &b,int &c) {
    if(a<b)
        swap(a,b);
    if(a<c)
        swap(a,c);
    if(b<c)
        swap(b,c);
}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&r[i]);
    sort(r+1,r+n+1);

    int cnt=1;
    node[cnt].num=1;
    node[cnt].now=r[1];
    for(int i=2; i<=n; i++) {
        if(node[cnt].now==r[i])
            node[cnt].num++;
        else {
            node[++cnt].now=r[i];
            node[cnt].num=1;
        }
    }

    priority_queue<Node> Q;
    for(int i=1; i<=cnt; i++)
        Q.push(node[i]);

    int sum=0;
    while(Q.size()>=3) {
        Node a=Q.top(); Q.pop();
        int aNow=a.now;
        a.num--;

        Node b=Q.top(); Q.pop();
        int bNow=b.now;
        b.num--;

        Node c=Q.top(); Q.pop();
        int cNow=c.now;
        c.num--;

        if(a.num>0)
            Q.push(a);
        if(b.num>0)
            Q.push(b);
        if(c.num>0)
            Q.push(c);

        Sort(aNow,bNow,cNow);
        res[++sum].a=aNow;
        res[sum].b=bNow;
        res[sum].c=cNow;
    }

    printf("%d\n",sum);
    for(int i=1; i<=sum; i++)
        printf("%d %d %d\n",res[i].a,res[i].b,res[i].c);
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值