题目链接:http://codeforces.com/contest/140/problem/C

题目大意:

有n个雪球(半径为:r1,r2,r3.....rn);一个雪人要三个雪球。但是要求半径两两不相同。
求可以堆雪人数量的最大值。

输入
第一行n代表了雪球数量。
第二行,n个数字代表了雪球的半径。

输出
第一行 k 代表了可以堆几个雪人
下面k行,输出每个雪人的雪球半径。(由大到小,空格隔开)。
各个雪人的顺序不定。

若有多种情况,输出其一。【应该指代k相同的情况下,有多种情况】

分析:

  很明显是贪心,不过贪心策略有待斟酌。
  一开始我想当然的把数据按大小排序后从小到大贪心,结果就Wa了,很容易找到反例:1 2 3 4 4 4 5 5 5
  如果从小到大贪,那么答案为1,不过这组数据眼睛看看答案都应该是3。造成这种情况的原因是我把数量少的先贪掉了,很多数量多的没得贪。因此贪心策略应该先贪数量多的。

代码如下:

 #include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ; struct Node{
int value, amount = ; inline bool operator < (const Node &x) const {
return amount < x.amount;
}
}; int n, nlen, ans;
Node nodes[maxN];
unordered_map< int, int > mii;
priority_queue< Node > maxH;
int Ans[maxN][]; int main(){
while(cin >> n) {
mii.clear();
nlen = ;
rep(i, n) {
int t;
scanf("%d", &t);
if(mii.find(t) != mii.end()) {
++nodes[mii[t]].amount;
}
else {
mii[t] = nlen;
nodes[nlen].value = t;
nodes[nlen++].amount = ;
}
} while(!maxH.empty()) maxH.pop();
rep(i, nlen) maxH.push(nodes[i]); ans = ; while(maxH.size() >= ) {
Node a = maxH.top();
maxH.pop();
Node b = maxH.top();
maxH.pop();
Node c = maxH.top();
maxH.pop(); int x = min(min(a.value, b.value), c.value);
int z = max(max(a.value, b.value), c.value);
int y = a.value + b.value + c.value - x - z;
Ans[ans][] = x;
Ans[ans][] = y;
Ans[ans++][] = z; if(--a.amount) maxH.push(a);
if(--b.amount) maxH.push(b);
if(--c.amount) maxH.push(c);
}
cout << ans << endl; rep(i, ans) printf("%d %d %d\n", Ans[i][], Ans[i][], Ans[i][]);
}
return ;
}

CodeForces 140C New Year Snowm的更多相关文章

  1. CodeForces 140C New Year Snowmen(堆)

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

  2. [Codeforces 140C] New Year Snowmen

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

  3. Codeforces 140C(二分、构造)

    要点 可以贪心选数量最多的那三个构造 二分的话里面的check我不太会.正解是既然当前答案为\(k\)个,那每个物品最多只会出现\(k\)次,多余的丢掉,剩下的总数如果大于等于\(3k\)则true. ...

  4. 【Codeforces 140C】New Year Snowmen

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

  5. [短期持续更新]Codeforces 构造题一览

    说实话我觉得做这种题很没意思(不够硬核), 可是人有短板终究是要补的...起码这种类型补起来相对简单 所以还是把先前准备好的专题放下吧,做点实现上比较休闲的题 ps.为了精简篇幅,代码全部丢到ubun ...

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. C# 1-2+3-4+5...+m的几种方法

    class Program { //第一种(1-2)+(3-4)+(5-6)...+m public static void Test(int m) { ; == ) { z = -(m / ); } ...

  2. [前端]css前端样式的模块化

    css样式文件结构( 模块划分的单入口 ) common|_ _ _ _ _ _reset.css|_ _ _ _ _ _common.css 公用样式 libs|_ _ _ _ _ _bootstr ...

  3. .NET MVC 简单的插件式开发

    插件式开发的优势 1.提高软件的复用度 2.提高软件开发的并行性 3.缩短软件的研发周期.节约研发成本,带给程序开发人员更多的灵活性,产品在软件发布以后还可以添加新的插件和完善已有的功能. 4.方便软 ...

  4. 轨迹系列5——验证轨迹GPS坐标转换为本地坐标的四/七参数是否准确的一种方案

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 目前对多个项目轨迹不准确的情况做了排查,发现导致轨迹偏移百分 ...

  5. RecycleView设置顶部分割线(记录一个坑)

    大家都知道,想给RecycleView设置分割线可以重写RecyclerView.ItemDecoration 项目过程中,遇到一个需求:RecycleView顶部有一条灰色的间隔,我想到了给Recy ...

  6. 在android studio中配置运行时签名

    做项目的时候,有时需要用到第三方接口,而基本第三方接口都是要求我们要先进行签名.结果每次调试都得手动进行签名一次,实在麻烦.所以android studio提供了一种在运行的时候自动进行签名的方法,在 ...

  7. C#中的yield return用法演示源码

    下边代码段是关于C#中的yield return用法演示的代码. using System;using System.Collections;using System.Collections.Gene ...

  8. 手机Soc芯片简介

    手机SoC(System On a Chip,在一个芯片里面集成CPU.GPU.SP.ISP.RAM内存.Wi-Fi控制器.基带芯片以及音频芯片等)芯片(基于arm架构指令集) 高通骁龙(Snapdr ...

  9. Filebeat使用内置的mysql模块收集日志存储到ES集群并使用kibana存储

    Filebeat内置了不少的模块,可以直接使用他们对日志进行收集,支持的模块如下: [root@ELK-chaofeng07 logstash]# filebeat modules list Enab ...

  10. LeetCode算法题-Minimum Index Sum of Two Lists(Java实现)

    这是悦乐书的第272次更新,第286篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第139题(顺位题号是599).假设Andy和Doris想要选择一家餐馆吃晚餐,他们都有 ...