题面

CodeForces

题解

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

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using std::min; using std::max;
using std::sort; using std::swap;
using std::unique; using std::lower_bound;
using std::priority_queue;
typedef long long ll; template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
} const int N = 1e5 + 10;
int n, a[N], b[N], zc[N], col[N];
struct Memb { int val, ind; };
struct Ans { int x, y, z; } A[N]; int tot;
inline bool operator < (const Memb &a, const Memb &b) { return a.val < b.val; }
priority_queue<Memb> q; int main () {
read(n); int m = n;
for(int i = 1; i <= n; ++i)
read(a[i]), b[i] = a[i];
sort(&b[1], &b[m + 1]), m = unique(&b[1], &b[m + 1]) - b - 1;
for(int i = 1; i <= n; ++i) {
int tmp = lower_bound(&b[1], &b[m + 1], a[i]) - b;
zc[tmp] = a[i], a[i] = tmp, ++col[tmp];
}
for(int i = 1; i <= m; ++i)
q.push((Memb){col[i], i});
Memb a_, b_, c_;
while(q.size() >= 3) {
a_ = q.top(); q.pop();
b_ = q.top(); q.pop();
c_ = q.top(); q.pop();
A[++tot] = (Ans){zc[a_.ind], zc[b_.ind], zc[c_.ind]};
if(--a_.val) q.push(a_);
if(--b_.val) q.push(b_);
if(--c_.val) q.push(c_);
}
printf("%d\n", tot);
for(int i = 1; i <= tot; ++i) {
if(A[i].y > A[i].x) swap(A[i].y, A[i].x);
if(A[i].z > A[i].y) swap(A[i].z, A[i].y);
if(A[i].y > A[i].x) swap(A[i].y, A[i].x);
if(A[i].z > A[i].y) swap(A[i].z, A[i].y);
printf("%d %d %d\n", A[i].x, A[i].y, A[i].z);
}
return 0;
}

CodeForces 140C New Year Snowmen(堆)的更多相关文章

  1. [Codeforces 140C] New Year Snowmen

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

  2. 【Codeforces 140C】New Year Snowmen

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

  3. CodeForces 140C New Year Snowm

    题目链接:http://codeforces.com/contest/140/problem/C 题目大意: 有n个雪球(半径为:r1,r2,r3.....rn):一个雪人要三个雪球.但是要求半径两两 ...

  4. 2018.12.05 codeforces 948C. Producing Snow(堆)

    传送门 维护一个堆. 每次先算出一个都不弹掉的总贡献. 然后把要弹掉的弹掉,并减去它们对应的贡献. 代码: #include<bits/stdc++.h> #define ri regis ...

  5. 【题解】Berland.Taxi Codeforces 883L 模拟 线段树 堆

    Prelude 题目传送门:ヾ(•ω•`)o Solution 按照题意模拟即可. 维护一个优先队列,里面装的是正在运营中的出租车,关键字是乘客的下车时间. 维护一个线段树,第\(i\)个位置表示第\ ...

  6. Codeforces 854C Planning(贪心+堆)

    贪心:让代价大的尽量移到靠前的位置. 做法:先让前k个数加进堆里,枚举k+1~n+k,每次把新元素加进堆后找到最大代价放在当前位置即可. #include<bits/stdc++.h> # ...

  7. Codeforces 1329C - Drazil Likes Heap(堆+贪心)

    题目链接 题意 给出一个高度为 h 的大根堆, 要求弹出其中若干个数后高度变为 g, 并且前后大根堆都是满二叉树. 问新的大根堆所有数之和的最小值, 并要给出一种弹出数的操作序列(节点序号). h, ...

  8. Codeforces 626G - Raffles(贪心+堆)

    题面传送门 考虑对于固定的彩票池 \(i\),我们假设现在押了 \(x\) 张彩票.利用差分的思想,从 \(x\) 张彩票变为 \(x+1\) 张时,期望的变化量 \(\Delta E=\dfrac{ ...

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

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

随机推荐

  1. 关于安装在win10上的oracle10g 兼容性问题

    首先在安装过程中会出现一次报错,在安装的时候 安装好了以后,准备敲击命令如果出现闪退,即是兼容性问题,下面继续设置兼容性问题 然后右键----属性----兼容性,勾上以兼容性运行即可

  2. Spring AOP注解为什么失效?90%Java程序员不知道

    使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效. 如下面几种场景 1.Controller直接调用Se ...

  3. 天梯赛 L2-007. (并查集) 家庭房产

    题目链接 题目描述 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式 ...

  4. 《区块链100问》第81集:应用类项目Augur

    Augur是基于以太坊区块链打造的去中心化预测平台,于2015年6月正式发布,是以太坊上的第一款应用. Augur采用了一个叫“群体智慧”的概念,它的意思是,一群人的智慧会高于这群人中最聪明的人.所以 ...

  5. docker 错误排查:无法进入容器.

    docker 错误排查:无法进入容器. #docker exec -it 3c1d bash rpc error: code = 2 desc = oci runtime error: exec fa ...

  6. Linux下简单粗暴使用rsync实现文件同步备份【转】

    这篇来说说如何安全的备份,还有一点不同的是上一篇是备份服务器拉取数据,这里要讲的是主服务器如何推送数据实现备份. 一.备份服务器配置rsync文件 vim /etc/rsyncd.conf #工作中指 ...

  7. juery下拉刷新,ajax请求,div加载更多元素(一)

    ;//设置当前页数 var flag=true; //滑动加载 $(function(){ var winH = $(window).height(); //页面可视区域高度 $(window).sc ...

  8. video标签、audio标签

    1.video标签 <video src="nans.mp4" controls="controls"  autoplay="autoplay& ...

  9. poj1102

    模拟 #include <iostream> #include <string> using namespace std; ][][] = { { ' ', '-', ' ', ...

  10. cuowu

    ngFor不能用于Object rowspan colspan不能绑定变量,要用attr.colspan https://stackoverflow.com/questions/35615751/wh ...