C. New Year Snowmen --优先队列
2015-07-29 02:53
507 查看
要求一堆雪球选出半径完全不同的三个即可做成一个雪人
看最多能做多少个雪人
先把雪球半径和对应个数统计好 打包(结构体) 进入优先队列(数量优先)
每次pop出前三个,
记录好,分别把对应雪球的个数减一(结构体) , 若个数为零,丢弃,否则继续进入队列
直到queue.size<3
看最多能做多少个雪人
先把雪球半径和对应个数统计好 打包(结构体) 进入优先队列(数量优先)
每次pop出前三个,
记录好,分别把对应雪球的个数减一(结构体) , 若个数为零,丢弃,否则继续进入队列
直到queue.size<3
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <iostream> using namespace std; int ans[100000+5][3]; int vis[100000+5]; struct node { int num,x; bool operator <( node b) const { return num<b.num; ///重载为 数量大优先 } }; priority_queue<node> qq;//使用默认优先级比较符号< int cmp(int a ,int b) { return a>b; //降序 } int cmp2(node a ,node b) { return a.x>b.x; //升序 } int main() { int n; cin>>n; int t; int i; for (i=1;i<=n;i++) { scanf("%d",&t); vis[i]=t; } sort(vis+1,vis+1+n,cmp); for (i=1;i<=n;i++) { int cun=1; while(vis[i]==vis[i+1]&&i<=n) { cun++; i++; } node t; t.num=cun; t.x=vis[i]; qq.push(t); //得到重复的个数 及该 数值 进入优先队列 } int ok=0; while(qq.size()>=3) //不同的雪球种类>=3 { node q[4]; q[1].x=q[2].x=q[3].x=0; q[1]=qq.top(),qq.pop(); //取出前三大,复杂度logn q[2]=qq.top(),qq.pop(); q[3]=qq.top(),qq.pop(); sort(q+1,1+q+3,cmp2); //把雪球的半径排序,升序 ans[++ok][0]=q[1].x; //记录答案 ans[ok][1]=q[2].x; ans[ok][2]=q[3].x; q[1].num--;q[2].num--;q[3].num--; //数量减一 if(q[1].num) //若个数为零,丢弃,否则继续进入队列 qq.push(q[1]); if (q[2].num) qq.push(q[2]); if (q[3].num) qq.push(q[3]); } printf("%d\n",ok); for (i=1;i<=ok;i++) { printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]); } return 0; }
相关文章推荐
- MyGUI 解析
- 封装 链表
- cf 442B Andrey and Problem
- Cocos2d-x中文支持问题的解决办法
- python总结
- python总结
- Android官方命令深入分析之AVD Manager
- Android官方命令深入分析之AVD Manager
- 中国大学MOOC-翁恺-C语言程序设计习题集 08-2 到 10-2
- JAVA中泛型的本质
- 八皇后问题
- 八皇后问题
- MVP设计模式的应用
- Android 官方命令深入分析之android
- Android 官方命令深入分析之android
- ubuntu mysql 无法启动 简单排查
- poj 3254 Corn Fields 【状压 DP 入门】
- 使用if语句和switch语句时,分号和变量作用域的问题总结
- Longest Valid Parentheses
- Uri获取文件路径