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

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]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值