Day3-E-New Year Snowmen CodeForces140C
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
7
1 2 3 4 5 6 7
2
3 2 1
6 5 4
3
2 2 3
0 思路:贪心,每次选出还剩的最大的三个组成一个雪人,用优先队列为数据结构,代码如下:
struct Node {
int id, sum;
Node(int _id = , int _sum = ) : id(_id), sum(_sum){}
bool operator<(const Node &a) const {
return sum < a.sum;
}
};
struct Snowman {
int a, b, c;
Snowman(int _a, int _b, int _c) : a(_a), b(_b), c(_c){}
};
bool comp(int a,int b) {
return a > b;
}
map<int, int> Cache;
vector<Snowman> res;
int n;
int main() {
scanf("%d", &n);
priority_queue<Node> q;
for (int i = ; i < n; ++i) {
int tmp;
scanf("%d", &tmp);
Cache[tmp]++;
}
for(auto i = Cache.begin(); i != Cache.end(); ++i)
q.push(Node(i->first,i->second));
while(q.size() > ) {
Node t1, t2, t3;
t1 = q.top(), q.pop();
t2 = q.top(), q.pop();
t3 = q.top(), q.pop();
res.push_back(Snowman(t1.id, t2.id, t3.id));
t1.sum--, t2.sum--, t3.sum--;
if(t1.sum > ) q.push(t1);
if(t2.sum > ) q.push(t2);
if(t3.sum > ) q.push(t3);
}
printf("%d\n", res.size());
for (int i = ; i < res.size(); ++i) {
int tmp[];
tmp[] = res[i].a, tmp[] = res[i].b, tmp[] = res[i].c;
sort(tmp, tmp + , comp);
for(int j = ; j < ; ++j) {
if(j)
printf(" ");
printf("%d", tmp[j]);
}
printf("\n");
}
return ;
}
Day3-E-New Year Snowmen CodeForces140C的更多相关文章
- 冲刺阶段day3
day3 项目进展 今天周三,我们五个人难得的一整个下午都能聚在一起.首先我们对昨天的成果一一地查看了一遍,并且坐出了修改.后面的时间则是做出 登录界面的窗体,完善了登录界面的代码,并且实现了其与数据 ...
- python笔记 - day3
python笔记 - day3 参考:http://www.cnblogs.com/wupeiqi/articles/5453708.html set特性: 1.无序 2.不重复 3.可嵌套 函数: ...
- python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理
python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...
- Spark菜鸟学习营Day3 RDD编程进阶
Spark菜鸟学习营Day3 RDD编程进阶 RDD代码简化 对于昨天练习的代码,我们可以从几个方面来简化: 使用fluent风格写法,可以减少对于中间变量的定义. 使用lambda表示式来替换对象写 ...
- Spark Tungsten揭秘 Day3 内存分配和管理内幕
Spark Tungsten揭秘 Day3 内存分配和管理内幕 恭喜Spark2.0发布,今天会看一下2.0的源码. 今天会讲下Tungsten内存分配和管理的内幕.Tungsten想要工作,要有数据 ...
- Catalyst揭秘 Day3 sqlParser解析
Catalyst揭秘 Day3 sqlParser解析 今天我们会进入catalyst引擎的第一个模块sqlparser,它是catalyst的前置模块. 树形结构 从昨天的介绍我们可以看到sqlPa ...
- Kakfa揭秘 Day3 Kafka源码概述
Kakfa揭秘 Day3 Kafka源码概述 今天开始进入Kafka的源码,本次学习基于最新的0.10.0版本进行.由于之前在学习Spark过程中积累了很多的经验和思想,这些在kafka上是通用的. ...
- python s12 day3
python s12 day3 深浅拷贝 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- Day3 - Python基础3 函数、递归、内置函数
Python之路,Day3 - Python基础3 本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8. ...
随机推荐
- 【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案
前言 之前已经写过一篇文章介绍SpringBoot整合Spring Cache,SpringBoot默认使用的是ConcurrentMapCacheManager,在实际项目中,我们需要一个高可用的. ...
- 802.1X高级配置
部署VLAN和ACLCisco Catalyst交换机(认证方)和Cisco ACS(认证服务器)具备动态分配VLAN或者ACL的能力.Cisco ACS可以将某个用户分配给指定的VLAN,或应用AC ...
- 转专业后补修C语言的一些体会(4)
1.对于文件的打开和关闭操作:首先了解到,C语言将文件分成了两种类型:文本文件和二进制文件.针对这两种文件,各有不同的文件读写方式.在C语言中,文件的操作要借助一个文件指针 即FILE 类型,定义了一 ...
- JAVA web课堂测试1
1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分)2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分)3性别:要求用单选框或下拉框实现 ...
- hive启动报错(整合spark)
spark整合hive后,hive启动报错: ls: cannot access /export/servers/spark/lib/spark-assembly-*.jar: No such fil ...
- Update(Stage4):scala补充知识
1.惰性加载: 在企业的大数据开发中,有时候会编写非常复杂的SQL语句,这些SQL语句可能有几百行甚至上千行.这些SQL语句,如果直接加载到JVM中,会有很大的内存开销.如何解决? 当有一些变量保存的 ...
- 【PAT甲级】1059 Prime Factors (25 分)
题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...
- iOS 混合开发之 Cordova 实践
在15年时,之前公司使用 Cordova 做混合开发使用,后来公司没有用到了,现在重新记录下. Cordova (官网:http://cordova.apache.org/)简介: Apache Co ...
- vs code插件大全
一.HTML Snippets 超级使用且初级的H5代码片段以及提示 二.HTML CSS Support 让HTML标签上写class智能提示当前项目所支持的样式 三.Debugger for C ...
- vue 组件,以及组件的复用
有时候代码的某一模块可能会经常使用到,那么完全可以把这一模块抽取出来,封装为一个组件,哪里需要用到的时候只需把模块调用即可 .参考vue官方 https://cn.vuejs.org/v2/guide ...