Codeforces140CNew Year Snowmen
一道优先队列好题
不难发现,每次取最多的几个雪球能保证凑出来的雪人最多,然后我们就先离散化一下,然后封装丢到优先队列里,每次取出队首的三个,将数量各自减一 ,表示凑了一个雪人,然后将数量大于一的再丢回去,直到剩余种数小于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相关推荐
- New Year Snowmen codeforces 140C
题目 As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowme ...
- L - New Year Snowmen
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. ...
- 贪心(优先队列) - New Year Snowmen - CodeForces - 140C
贪心(优先队列) - New Year Snowmen - CodeForces - 140C 题意: 给定一个长度为n的正整数序列a1,a2,...,an.给定一个长度为n的正整数序列a_1,a_2 ...
- E - New Year Snowmen
题目链接:https://cn.vjudge.net/contest/272792#problem/E E - New Year Snowmen As meticulous Gerald sets t ...
- New Year Snowmen(贪心)
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. ...
- New Year Snowmen((贪心)map+优先队列)
文章目录 一.题目 二.解题步骤 1.题意 2.思路 Source Program 一.题目 New Year Snowmen:传送门 Examples Input 7 1 2 3 4 5 6 7 O ...
- CodeForces - 140C New Year Snowmen
CodeForces - 140C New Year Snowmen 题意: 现在来做雪人,每个雪人由三个不同大小的雪球构成:一个大的,一个中等的,一个小的.现在有 n 个雪球半径分别为 r1, r2 ...
- CF140C-New Year Snowmen【优先队列】
正题 题目链接:https://www.luogu.com.cn/problem/CF140C 题目大意 nnn个雪球,一个雪人需要用333个不同大小的雪球堆起,求最多雪人. 解题思路 我们每次拿相同 ...
- Winter And Snowmen
https://vjudge.net/problem/TopCoder-12891 暴力想法是:dp[i][s1][s2]前i个,第一个集合xor是s1,第二个集合xor是s2方案数O(n^3) 有x ...
最新文章
- 路由 php mysql_Php 连接 MySQL 的三种方式
- Java中List高效去重
- php实现微信清粉功能,PHP实现微信提现功能
- H5开发的web APP和原生APP的区别
- c语言中主函数创建链表,主函数怎么调用函数(数据结构,创建链表)
- OnItemClickListener,OnScrollListener应用
- 【数据库】SQL语句
- Python版常见的排序算法
- Runtime Library
- 一生温暖纯良,不舍爱与自由
- IDEA导入Git中项目
- (转)金融“核武器”即将引爆整个行业
- java模拟器1.4.2_j2me模拟器下载
- Websphere9.0的安装
- pytorch中tensor.topk
- iphonex蓝牙打不开转圈_iphonex蓝牙一直在转圈
- 大数据和人工智能的关系,超全解析 1
- UVa 11437 (梅涅劳斯定理) Triangle Fun
- 黑盒测试和白盒测试的优缺点
- idea一直卡在build不动