CodeForces 140C - New Year Snowmen(优先队列)

本篇博客介绍了一道经典的算法题目——雪人构造问题。题目要求从给定的多个半径不同的雪球中构造尽可能多的雪人,每个雪人由三个不同大小的雪球组成。文章提供了详细的解题思路及AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. New Year Snowmen
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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个数,输出若干3个一组的严格递减数,并输出组的个数。

解题思路:
放到优先队列中,每次输出最大的。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
struct node
{
    int r;
    int num;
    friend bool operator < (node a,node b) {return a.num<b.num;}
}q;
struct node2
{
    int r1;
    int r2;
    int r3;
}ans[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    map<int,int>mp;
    mp.clear();
    for(int i = 0;i < n;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        mp[tmp]++;
    }
    int cnt = 0;
    priority_queue<node> p;
    map<int,int>::iterator it;
    for(it = mp.begin();it != mp.end();it++)
    {
        node q;
        q.r = it->first;
        q.num = it->second;
        p.push(q);
    }
    while(!p.empty())
    {
        node t1 = p.top();
        p.pop();
        if(p.empty())   break;
        node t2 = p.top();
        p.pop();
        if(p.empty())   break;
        node t3 = p.top();
        p.pop();
        ans[cnt].r1 = t1.r,t1.num--;
        ans[cnt].r2 = t2.r,t2.num--;
        ans[cnt].r3 = t3.r,t3.num--;
        cnt++;
        if(t1.num)  p.push(t1);
        if(t2.num)  p.push(t2);
        if(t3.num)  p.push(t3);
    }
    printf("%d\n",cnt);
    int tmp[3];
    for(int i = 0;i < cnt;i++)
    {
        tmp[0] = ans[i].r1;
        tmp[1] = ans[i].r2;
        tmp[2] = ans[i].r3;
        sort(tmp,tmp+3);
        printf("%d %d %d\n",tmp[2],tmp[1],tmp[0]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值