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

inputCopy

7

1 2 3 4 5 6 7

outputCopy

2

3 2 1

6 5 4

inputCopy

3

2 2 3

outputCopy

0

一开始想暴力,奈何10^9过不了,这是有贪心策略的,既然要使的堆成雪人的数量最多,比如 6 3 3 3 2 2 2 2 1 1,要想使的雪人数量最多,应该尽可能的让2 分到更多组,但是每个雪球只能用一次,当2用的比3少的时候,应该改优先为3,所以应该写一个结构体,先按照数量排序,再按半径排序,每次拿前三个即可。

尴尬:取完一定要放回去,还要是数量减一。

#include <bits/stdc++.h>
using namespace std;
struct Node{
int arr,num; //半径,数目
}node[100005];
map <int,int> M;
priority_queue <Node> Q; //默认大根队列,先出队列的为最大的
vector <int> V[3]; bool operator < (Node a,Node b){
return a.num < b.num;
}
int main()
{
int n;
while(~scanf("%d",&n)){
while(!Q.empty())
Q.pop();
M.clear();
for(int i = 0;i < 3;i ++) V[i].clear();
int temp;
for(int i = 1;i <= n;i ++){
scanf("%d",&temp)
M[temp]++;
}
int len = 0;
map <int,int>::iterator it;
for(it = M.begin();it != M.end();it ++){ //结构体存半径和数目
node[len].arr = it->first;
node[len++].num = it->second;
}
for(int i = 0;i < len;i ++) //入队列
Q.push(node[i]);
len = 0;
while(!Q.empty()){
Node res1 = Q.top();
Q.pop();
if(Q.empty()) break;
Node res2 = Q.top();
Q.pop();
if(Q.empty()) break;
Node res3 = Q.top();
Q.pop();
int temp[3] = {res1.arr,res2.arr,res3.arr};
sort(temp,temp+3);
V[0].push_back(temp[2]);
V[1].push_back(temp[1]);
V[2].push_back(temp[0]);
res1.num--,res2.num--,res3.num--;
if(res1.num) Q.push(res1); //数目不为0时继续入队列
if(res2.num) Q.push(res2);
if(res3.num) Q.push(res3);
}
cout<<V[0].size()<<endl;
for(int i = 0;i < V[0].size();i ++)
cout<<V[0][i]<<" "<<V[1][i]<<" "<<V[2][i]<<endl;
}
return 0;
}

Codeforce 140C (贪心+优先队列)补题的更多相关文章

  1. 51nod 1163 最高的奖励(贪心+优先队列)

    题目链接:51nod 1163 最高的奖励 看着这题我立马就想到昨天也做了一道贪心加优先队列的题了奥. 按任务最晚结束时间从小到大排序,依次选择任务,如果该任务最晚结束时间比当前时间点晚,则将该任务的 ...

  2. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  3. 2018 CCPC 桂林站(upc复现赛)补题

    2018 CCPC 桂林站(upc复现赛)补题 G.Greatest Common Divisor(思维) 求相邻数的差值的gcd,对gcd分解素因子,对所有的素因子做一次遍历,找出最小答案. 几个样 ...

  4. 【cf补题记录】Codeforces Round #608 (Div. 2)

    比赛传送门 再次改下写博客的格式,以锻炼自己码字能力 A. Suits 题意:有四种材料,第一套西装需要 \(a\).\(d\) 各一件,卖 \(e\) 块:第二套西装需要 \(b\).\(c\).\ ...

  5. 【cf补题记录】Codeforces Round #607 (Div. 2)

    比赛传送门 这里推荐一位dalao的博客-- https://www.cnblogs.com/KisekiPurin2019/ A:字符串 B:贪心 A // https://codeforces.c ...

  6. 第十届山东省acm省赛补题(2)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124 L Median Time Limit: 1 Second      ...

  7. 【补题记录】ZJU-ICPC Summer Training 2020 部分补题记录

    补题地址:https://zjusummer.contest.codeforces.com/ Contents ZJU-ICPC Summer 2020 Contest 1 by Group A Pr ...

  8. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

  9. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  10. hdu5017:补题系列之西安网络赛1011

    补题系列之西安网络赛1011 题目大意:给定一个椭球: 求它到原点的最短距离. 思路: 对于一个椭球的标准方程 x^2/a^2 + y^2/b^2 +z^2/c^2=1 来说,它到原点的最短距离即为m ...

随机推荐

  1. python通过pymysql实现数据库的增删改查

    今天实现了python对于数据库的增删改查工作: 具体的每一步的数据库的增删盖茶自己都实验过了. 掌握了基本的数据库操作语言.然后就是对于tuple()的认识. 以及对于查询语句中的:fetchone ...

  2. zendframewor 项目构建

    1.安装好新的php 2.安装composer   在https://getcomposer.org/download/上手动下载最新版本的composer.phar   放到/usr/local/b ...

  3. 2018蓝桥杯省赛(C/C++ C组)

    因进考场不让带优盘,顾想不起有些啥题了,静待更新吧! 再次强调C++最新标准,main函数必须指定返回类型为int,且返回值最好为0(人走的多了就是路了,有人偏返回999那也没办法) 1.大概是小明给 ...

  4. [go]map基本使用和底层原理

    1.map基本使用 map声明 var m4 map[int]int //只是声明 没有开辟空间 m4[1]=100 //报错 log.Println(m4) 创建 //1 m3:=make(map[ ...

  5. 用robotframework 标准库String解决由于存在千分位分隔符导致两个数值不相等的问题。

    在编写robotframework自动化断言的过程中,我遇到了如下问题: 我想写一个两个金额判断是否相等的断言,其中一个金额是展示字段存在千分位分隔符,另一个金额是input带入字段,没有千分位分隔符 ...

  6. ffmpeg使用ss与t参数对视频进行剪辑

    ffmpeg能够实现对视频进行剪辑操作,ss为指定视频剪切开头的起始时间,t制定视频的总长度,ss与t的单位均为:秒. ffmpeg -ss 7200 -i 1080p.mp4 -c copy -t ...

  7. 007-函数-C语言笔记

    007-函数-C语言笔记 学习目标 1.[了解]函数的分类 2.[掌握]函数的声明定义和调用 3.[掌握]函数的形参和实参 4.[掌握]带返回值的函数 5.[掌握]全局变量和局部变量 6.[了解]注释 ...

  8. threejs创建地球

    上个月底,在朋友圈看到一个号称“这可能是地球上最美的h5”的分享,点进入后发现这个h5还很别致,思考了一会,决定要不高仿一个? 到今天为止,高仿基本完成, 线上地址 github地址 除了手机端的me ...

  9. Python-selenium安装与Java-selenium安装

    一.Python安装及selenium的安装 1.安装Pythonhttps://www.Python.org2.安装setuptools.distribute.piphttps://pypi.pyt ...

  10. 页面性能分析-Chrome Dev Tools

    一.分析面板介绍 进行页面性能快速分析的主要是图中圈出来的几个模块功能: Network : 页面中各种资源请求的情况,这里能看到资源的名称.状态.使用的协议(http1/http2/quic...) ...