小陈老师、雪人 HRBUST - 1176(优先队列+)

东北的冬季,尤其是过年的时候,小陈老师喜欢去堆雪人。
每个雪人主要由三个雪球构成:大雪球、中雪球、小雪球。
他已经准备好了N个雪球,半径分别等于r1, r2, …, rn。如果要堆一个雪人,就需要三个半径互不相等的雪球。
例如:
三个雪球的半径为1、2、3,能够用来堆一个雪人。但是半径为2、2、3或者2、2、2的三个雪球就不可以。
快帮帮小陈老师,算算他最多能用这些雪球堆多少个雪人。
Input
对于每组测试数据:
第1行,包含一个整数n(1≤n≤100000) — 雪球的数量。
第2行,包含n个整数 — 雪球的半径r1, r2, …, rn (1≤ri≤1000000000)。
处理到文件结束
Output
对于每组测试数据:
第1行,输出最多能堆多少雪人 - k。
接下来k行,每行描述一个雪人,每行用空格分割三个数字分别表示大雪球、中雪球、小雪球的半径。
可以用任何顺序输出每个雪人。如果有多种可行解,输出任意一个即可。
Sample Input
7
1 2 3 4 5 6 7
3
2 2 3
Sample Output
2
3 2 1
6 5 4
0

思路:每次都取数量最大的前三个,所以优先队列

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#define ll long long

using namespace std;
const int M=365*24*60*60;
const int N=1e5+5;
struct node
{
    int x,y;
    bool operator<(const node &t)const
    {
        return y<t.y;
    }
};
int n,x,b[N][3];

int main()
{
    while(cin >> n)
    {
        map<int,int> m;
        while(n--)
        {
            cin >> x;
            m[x]++;
        }
        priority_queue<node> q;
        for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
        {
            q.push({it->first,it->second});
        }
        int res=0,c[3];
        while(!q.empty())
        {
            node t1=q.top();
            q.pop();
            if(q.empty())
                break;
            node t2=q.top();
            q.pop();
            if(q.empty())
                break;
            node t3=q.top();
            q.pop();
            c[0]=t1.x,c[1]=t2.x,c[2]=t3.x;
            sort(c,c+3);
            for(int i=2;i>=0;i--)
                b[res][2-i]=c[i];
            res++;
            if(--t1.y)
                q.push(t1);
            if(--t2.y)
                q.push(t2);
            if(--t3.y)
                q.push(t3);
        }
        printf("%d\n",res);
        for(int i=0;i<res;i++)
        {
            for(int j=0;j<3;j++)
                printf("%d ",b[i][j]);
            printf("\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值