codeforces 140C New Year Snowmen

 New Year Snowmen

 

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 whatmaximum 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

 

#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct Answer
{
    int idx1,idx2,idx3;
    Answer(){}
    Answer(int _idx1,int _idx2,int _idx3)
    {
        idx1=_idx1,idx2=_idx2,idx3=_idx3;
        if(idx1<idx2)swap(idx1,idx2);
        if(idx1<idx3)swap(idx1,idx3);
        if(idx2<idx3)swap(idx2,idx3);
    }
    void show(){printf("%d %d %d\n",idx1,idx2,idx3);}
};
struct Node
{
    int cnt,val;
    bool operator < (const Node& t)const
    {
        return cnt<t.cnt;
    }
};
const int N=1e5+5;
int r[N];
Node cnt[N];
vector<Answer>ans;
priority_queue<Node>pq;
int min(Node a,Node b,Node c)
{
    return min(a.cnt,min(b.cnt,c.cnt));
}
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 id=0;
    for(int i=1;i<=n;i++)
    {
        if(r[i]!=r[i-1])cnt[++id].val=r[i];
        cnt[id].cnt++;
    }
    for(int i=1;i<=id;i++)
        pq.push(cnt[i]);
    if((int)pq.size()<3)
    {
        puts("0");
        return 0;
    }
    while(1)
    {
        Node p1,p2,p3;
        p1=pq.top();pq.pop();
        p2=pq.top();pq.pop();
        p3=pq.top();pq.pop();
        if(p1.cnt>0&&p2.cnt>0&&p3.cnt>0)
            ans.push_back(Answer(p1.val,p2.val,p3.val));
        else break;
        p1.cnt--,p2.cnt--,p3.cnt--;
        pq.push(p1),pq.push(p2),pq.push(p3);
    }
    int len=ans.size();
    printf("%d\n",len);
    for(int i=0;i<len;i++)ans[i].show();
    return 0;
}

 

posted on 2016-07-27 20:36    阅读(209)  评论(0编辑  收藏  举报

导航