L - 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 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();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值