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,所以应该写一个结构体,先按照数量排序,再按半径排序,每次拿前三个即可。

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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int arr,num; //半径,数目
  5. }node[100005];
  6. map <int,int> M;
  7. priority_queue <Node> Q; //默认大根队列,先出队列的为最大的
  8. vector <int> V[3];
  9. bool operator < (Node a,Node b){
  10. return a.num < b.num;
  11. }
  12. int main()
  13. {
  14. int n;
  15. while(~scanf("%d",&n)){
  16. while(!Q.empty())
  17. Q.pop();
  18. M.clear();
  19. for(int i = 0;i < 3;i ++) V[i].clear();
  20. int temp;
  21. for(int i = 1;i <= n;i ++){
  22. scanf("%d",&temp)
  23. M[temp]++;
  24. }
  25. int len = 0;
  26. map <int,int>::iterator it;
  27. for(it = M.begin();it != M.end();it ++){ //结构体存半径和数目
  28. node[len].arr = it->first;
  29. node[len++].num = it->second;
  30. }
  31. for(int i = 0;i < len;i ++) //入队列
  32. Q.push(node[i]);
  33. len = 0;
  34. while(!Q.empty()){
  35. Node res1 = Q.top();
  36. Q.pop();
  37. if(Q.empty()) break;
  38. Node res2 = Q.top();
  39. Q.pop();
  40. if(Q.empty()) break;
  41. Node res3 = Q.top();
  42. Q.pop();
  43. int temp[3] = {res1.arr,res2.arr,res3.arr};
  44. sort(temp,temp+3);
  45. V[0].push_back(temp[2]);
  46. V[1].push_back(temp[1]);
  47. V[2].push_back(temp[0]);
  48. res1.num--,res2.num--,res3.num--;
  49. if(res1.num) Q.push(res1); //数目不为0时继续入队列
  50. if(res2.num) Q.push(res2);
  51. if(res3.num) Q.push(res3);
  52. }
  53. cout<<V[0].size()<<endl;
  54. for(int i = 0;i < V[0].size();i ++)
  55. cout<<V[0][i]<<" "<<V[1][i]<<" "<<V[2][i]<<endl;
  56. }
  57. return 0;
  58. }

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. uni-app商城项目(01)

    1.项目准备: 1.新建项目,清理项目结构 2.完成项目初始化配置. 2.项目开始阶段: 1.完成tabBar配置,新建需要的页面 2.在 '/utis'封装需要的发送请求api,有利于功能的实现. ...

  2. Python设计模式(1)-简单工厂模式

    为操作数据库设计增删改查操作 # coding=utf-8class DbManager: def __init__(self): pass def operate_db(self): pass cl ...

  3. CH5701 开车旅行(倍增dp+set)

    传送门 解题思路: 一道比较有趣的题,解题工作主要分为两块: ①找出k(k=0表示小A先走,k=1表示小B先走,下面同理)从点i出发下一个到达的点to[k][i]; 一开始偷懒用了vector(偷懒一 ...

  4. PHP远程代码执行漏洞复现(CVE-2019-11043)

    漏洞描述 CVE-2019-11043 是一个远程代码执行漏洞,使用某些特定配置的 Nginx + PHP-FPM 的服务器存在漏洞,可允许攻击者远程执行代码. 向Nginx + PHP-FPM的服务 ...

  5. CVE-2020-1938:Apache-Tomcat-Ajp漏洞-复现

    0x00 漏洞简介 Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行. Apache Tomcat服务器存在文件包含 ...

  6. Salesforce LWC学习(十六) Validity 在form中的使用浅谈

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/lightning-input/documentation h ...

  7. JavaScript_Array

    Array 概念特点 值的有序集合: 每一个值叫一个元素: 每个元素在数组中有一个位置,以数字表示,称为索引(下标): 元素可以是任何类型 索引从0开始,最大为2的32次方 数组的创建 数组直接量 v ...

  8. Git 创建远程仓库并克隆到本地,创建本地仓库并推送到远程仓库

    配置用户信息 配置的是你个人的用户名称和电子邮件地址.这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,会随更新内容一起被永久纳入历史记录 git config --glo ...

  9. [html][javascript] 关于SVG环形进度条

    下面是个例子: <style> .demo2{ transform-origin: center; transform: rotate(-90deg); transition: strok ...

  10. tensorflow1.0 变量加法

    import tensorflow as tf state = tf.Variable(0,name='counter') print(state.name) one = tf.constant(1) ...