一道优先队列好题

不难发现,每次取最多的几个雪球能保证凑出来的雪人最多,然后我们就先离散化一下,然后封装丢到优先队列里,每次取出队首的三个,将数量各自减一 ,表示凑了一个雪人,然后将数量大于一的再丢回去,直到剩余种数小于3为止

代码

//By AcerMo
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=100500;
int n,a[M],b[M],c[M];
struct emm
{int v,id;bool friend operator < (emm a,emm b){return a.v<b.v;}
}ad;
struct an{int a,b,c;}e[M];
priority_queue<emm>q;
inline int read()
{int x=0;char ch=getchar();while (ch>'9'||ch<'0') ch=getchar();while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x;
}
signed main()
{n=read();int cnt=0,s=0,tot=0;for (int i=1;i<=n;i++) a[i]=read();sort(a+1,a+n+1);for (int i=1;i<=n;i++)if (a[i]==a[i-1]) b[cnt]++;else b[++cnt]++,c[cnt]=a[i];for (int i=1;i<=cnt;i++)ad.id=c[i],ad.v=b[i],q.push(ad);if (q.size()<3) return puts("0"),0;while (q.size()>=3){emm x=q.top();q.pop();emm y=q.top();q.pop();emm z=q.top();q.pop();s++;x.v--;y.v--;z.v--;e[++tot].a=x.id;e[tot].b=y.id;e[tot].c=z.id;if (e[tot].a<e[tot].b) swap(e[tot].a,e[tot].b);if (e[tot].a<e[tot].c) swap(e[tot].a,e[tot].c);if (e[tot].b<e[tot].c) swap(e[tot].b,e[tot].c);if (x.v>0) q.push(x);if (y.v>0) q.push(y);if (z.v>0) q.push(z);}printf("%d\n",s);for (int i=1;i<=tot;i++)printf("%d %d %d\n",e[i].a,e[i].b,e[i].c);return 0;
}

Codeforces140CNew Year Snowmen相关推荐

  1. New Year Snowmen codeforces 140C

    题目 As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowme ...

  2. L - New Year Snowmen

    As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. ...

  3. 贪心(优先队列) - New Year Snowmen - CodeForces - 140C

    贪心(优先队列) - New Year Snowmen - CodeForces - 140C 题意: 给定一个长度为n的正整数序列a1,a2,...,an.给定一个长度为n的正整数序列a_1,a_2 ...

  4. E - New Year Snowmen

    题目链接:https://cn.vjudge.net/contest/272792#problem/E E - New Year Snowmen As meticulous Gerald sets t ...

  5. New Year Snowmen(贪心)

    As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. ...

  6. New Year Snowmen((贪心)map+优先队列)

    文章目录 一.题目 二.解题步骤 1.题意 2.思路 Source Program 一.题目 New Year Snowmen:传送门 Examples Input 7 1 2 3 4 5 6 7 O ...

  7. CodeForces - 140C New Year Snowmen

    CodeForces - 140C New Year Snowmen 题意: 现在来做雪人,每个雪人由三个不同大小的雪球构成:一个大的,一个中等的,一个小的.现在有 n 个雪球半径分别为 r1, r2 ...

  8. CF140C-New Year Snowmen【优先队列】

    正题 题目链接:https://www.luogu.com.cn/problem/CF140C 题目大意 nnn个雪球,一个雪人需要用333个不同大小的雪球堆起,求最多雪人. 解题思路 我们每次拿相同 ...

  9. Winter And Snowmen

    https://vjudge.net/problem/TopCoder-12891 暴力想法是:dp[i][s1][s2]前i个,第一个集合xor是s1,第二个集合xor是s2方案数O(n^3) 有x ...

最新文章

  1. 路由 php mysql_Php 连接 MySQL 的三种方式
  2. Java中List高效去重
  3. php实现微信清粉功能,PHP实现微信提现功能
  4. H5开发的web APP和原生APP的区别
  5. c语言中主函数创建链表,主函数怎么调用函数(数据结构,创建链表)
  6. OnItemClickListener,OnScrollListener应用
  7. 【数据库】SQL语句
  8. Python版常见的排序算法
  9. Runtime Library
  10. 一生温暖纯良,不舍爱与自由
  11. IDEA导入Git中项目
  12. (转)金融“核武器”即将引爆整个行业
  13. java模拟器1.4.2_j2me模拟器下载
  14. Websphere9.0的安装
  15. pytorch中tensor.topk
  16. iphonex蓝牙打不开转圈_iphonex蓝牙一直在转圈
  17. 大数据和人工智能的关系,超全解析 1
  18. UVa 11437 (梅涅劳斯定理) Triangle Fun
  19. 黑盒测试和白盒测试的优缺点
  20. idea一直卡在build不动

热门文章

  1. IPAD/IOS 访问限制密码找回方法(设置-通用-访问限制-密码的非刷机找回方法)
  2. html行内样式 修改,HTML的行内样式演示案例
  3. 卸载CTEX后再安装LaTex时会遇到的问题
  4. think在PHP中什么意思,thinkphp框架是什么意思
  5. 智慧社区可研究性报告
  6. 程序员非常实用的十个工具网站,值得收藏
  7. WordPress绑定多个域名和禁止搜索引擎收录非主域名的方法
  8. uniapp 获取到当前位置信息显示在地图上
  9. mean-shift均值偏移算法
  10. 罗马数字转整数(C++)