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. MTK Android Driver :Lcm

    MTK Android Driver :lcm 1.怎样新建一个LCD驱动 LCD模组主要包括LCD显示屏和驱动IC.比如LF040DNYB16a模组的驱动IC型号为NT35510.要在MTK6577 ...

  2. python3(十七) nonameFunc

    L = list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])) print(L) # [1, 4, 9, 16, 25, 36, 49, 64, ...

  3. Maybatis的一些总结(三:增删改查)

    回顾一个点 之前不懂这句: UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 现在理解了一点点,相当于实现了userMap ...

  4. Pytest系列(21)- allure的特性,@allure.description()、@allure.title()的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 前面介绍了两种allure的 ...

  5. 数论-质因数(gcd) UVa 10791 - Minimum Sum LCM

    https://vjudge.net/problem/UVA-10791/origin 以上为题目来源Google翻译得到的题意: 一组整数的LCM(最小公倍数)定义为最小数,即 该集合的所有整数的倍 ...

  6. EFCore.Sharding(EFCore开源分表框架)

    EFCore.Sharding(EFCore开源分表框架) 简介 引言 开始 准备 配置 使用 按时间自动分表 性能测试 其它简单操作(非Sharing) 总结 简介 本框架旨在为EF Core提供S ...

  7. C#——继承

    在某基类中声明 virtual 并在一个或多个派生类中被重新定义的成员函数称为虚函数. 虚函数的作用就是实现多态性(Polymorphism),多态性是将接口与实现进行分离. C#作为完全面向对象语言 ...

  8. Python父类和子类关系/继承

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @File:继承_子类和父类的关系.py @E-mail:364942 ...

  9. 论JDK5/7/8版本都做出了哪些革新

    在Java发展的里程碑上,有三个版本做出的改动,是革命性的 为什么说是革命性的呢? 因为这三个版本所推出的有些新机制,在之后的Java框架开发.新类的产生等等中, 都被广泛使用了. 那么,这三个版本的 ...

  10. webpack3 babel相关

    babel 链接地址 在index.js中写入js6的语法如 let fn = (){ console.log('this is es6') } 执行npm run build 在打包出来的js文件中 ...