题目大意:有n个已知半径的雪球。堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人?

题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多。

代码如下:

# include<iostream>
# include<map>
# include<vector>
# include<cstdio>
# include<queue>
# include<algorithm>
using namespace std; struct Node
{
int val,cnt;
Node(int _val,int _cnt):val(_val),cnt(_cnt){}
bool operator < (const Node &a) const{
return cnt<a.cnt;
}
};
int ans[40005][3],n;
map<int,int>mp;
priority_queue<Node>q; void solve()
{
int cnt=0;
while(!q.empty()){
Node a=q.top();
q.pop();
if(q.empty()) break;
Node b=q.top();
q.pop();
if(q.empty()) break;
Node c=q.top();
q.pop();
ans[cnt][0]=a.val;
ans[cnt][1]=b.val;
ans[cnt][2]=c.val;
++cnt;
if(a.cnt-1>0) q.push(Node(a.val,a.cnt-1));
if(b.cnt-1>0) q.push(Node(b.val,b.cnt-1));
if(c.cnt-1>0) q.push(Node(c.val,c.cnt-1));
}
printf("%d\n",cnt);
for(int i=0;i<cnt;++i){
sort(ans[i],ans[i]+3);
printf("%d %d %d\n",ans[i][2],ans[i][1],ans[i][0]);
}
} int main()
{
int a;
while(~scanf("%d",&n))
{
mp.clear();
for(int i=0;i<n;++i){
scanf("%d",&a);
++mp[a];
}
map<int,int>::iterator it;
while(!q.empty()) q.pop();
for(it=mp.begin();it!=mp.end();++it){
q.push(Node(it->first,it->second));
}
solve();
}
return 0;
}

  

CoderForce 140C-New Year Snowmen(贪心)的更多相关文章

  1. [Codeforces 140C] New Year Snowmen

    [题目链接] https://codeforces.com/problemset/problem/140/C [算法] 显然 , 我们每次应优先考虑数量多的雪球 将雪球个数加入堆中 , 每次取出数量前 ...

  2. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

  3. CodeForces 140C New Year Snowmen(堆)

    题面 CodeForces 题解 因为要保证两两不同,所以不能单纯的开堆来维护,堆维护一个二元组,个数为第一关键字,编号为第二关键字,对于一个相同的颜色,统计一下这个颜色的个数再用堆来维护就好了. # ...

  4. Codeforce 140C (贪心+优先队列)补题

    C. New Year Snowmen time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  5. CoderForce 141C-Queue (贪心+构造)

    题目大意:一个队伍,每个人只记得前面比他高的人的个数x.现在将队伍散开,问能否构造出一支满足条件的队伍,如果能,再给每个人一个满足题意的身高. 题目分析:一个一个排,x越少越先排,如果x比已经排好的人 ...

  6. 【Codeforces 140C】New Year Snowmen

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 每次都选择剩余个数最多的3个不同数字组成一组. 优先消耗剩余个数多的数字 这样能尽量让剩余的数字总数比较多,从而更加可能得到更多的3个组合 [ ...

  7. Day3-E-New Year Snowmen CodeForces140C

    As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. ...

  8. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  9. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. [分享] 采用opencv_cascadetrain进行训练的步骤及注意事项 [复制链接]

    http://f.dataguru.cn/thread-725364-1-1.html 很有用的一个帖子 转自:http://blog.csdn.net/xidianzhimeng/article/d ...

  2. Python3.x:抓取百事糗科段子

    Python3.x:抓取百事糗科段子 实现代码: #Python3.6 获取糗事百科的段子 import urllib.request #导入各类要用到的包 import urllib import ...

  3. c++的各种类型转换方式

    const_cast 用于去掉const属性,把const类型的指针变为非const类型的指针,如:const int *fun(int x,int y){} int *ptr=const_cast& ...

  4. Total Difference String

    Total Difference Strings 给一个string列表,判断有多少个不同的string,返回个数相同的定义:字符串长度相等并从左到右,或从右往左是同样的字符 abc 和 cba 为视 ...

  5. 01: shell基本使用

    目录: 1.1 编写登录欢迎脚本 1.2 重定向与管道操作 1.3 使用shell变量 1.4 特殊的shell变量 1.5 read与echo使用比较 1.1 编写登录欢迎脚本返回顶部 (1)新建脚 ...

  6. C++ 单例模式(转载)

    转载:http://www.cnblogs.com/cxjchen/p/3148582.html 转载:http://blog.csdn.net/hackbuteer1/article/details ...

  7. MNIST机器学习入门【学习笔记】

    平台信息:PC:ubuntu18.04.i5.anaconda2.cuda9.0.cudnn7.0.5.tensorflow1.10.GTX1060 作者:庄泽彬(欢迎转载,请注明作者) 说明:本文是 ...

  8. Linq join right join left join

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  9. JavaScript:值类型 引用类型

    JavaScript类型 (1)值类型:数值.布尔值.null.undefined. (2)引用类型:对象.数组.函数.  1.string var str="hongda"; v ...

  10. TeeChart设置图表的标题

    TeeChart的图表的标题设置方法 tChart1.Header.Text = "图表"; tChart1.Header.Lines = new string[] { " ...