哈理工OJ 1176 小陈老师、雪人(排序+优先队列模拟)
2017-01-10 11:05
435 查看
题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1176
小陈老师、雪人
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 244(61 users) Total Accepted: 85(48 users) Rating: Special Judge: Yes
Description
东北的冬季,尤其是过年的时候,小陈老师喜欢去堆雪人。
每个雪人主要由三个雪球构成:大雪球、中雪球、小雪球。
他已经准备好了N个雪球,半径分别等于r1, r2, …, rn。如果要堆一个雪人,就需要三个半径互不相等的雪球。
例如:
三个雪球的半径为1、2、3,能够用来堆一个雪人。但是半径为2、2、3或者2、2、2的三个雪球就不可以。
快帮帮小陈老师,算算他最多能用这些雪球堆多少个雪人。
Input
对于每组测试数据:
第1行,包含一个整数n(1≤n≤100000) — 雪球的数量。
第2行,包含n个整数 — 雪球的半径r1, r2, …, rn (1≤ri≤1000000000)。
处理到文件结束
Output
对于每组测试数据:
第1行,输出最多能堆多少雪人 - k。
接下来k行,每行描述一个雪人,每行用空格分割三个数字分别表示大雪球、中雪球、小雪球的半径。
可以用任何顺序输出每个雪人。如果有多种可行解,输出任意一个即可。
Sample Input
7
1 2 3 4 5 6 7
3
2 2 3
Sample Output
2
3 2 1
6 5 4
0
Author
齐达拉图
【思路分析】先把所有的雪球排序,然后把每种雪球存到一个优先队列里面,数量多的在前面,然后一直贪心啊,乱搞啊就好了。
【AC代码】
小陈老师、雪人
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 244(61 users) Total Accepted: 85(48 users) Rating: Special Judge: Yes
Description
东北的冬季,尤其是过年的时候,小陈老师喜欢去堆雪人。
每个雪人主要由三个雪球构成:大雪球、中雪球、小雪球。
他已经准备好了N个雪球,半径分别等于r1, r2, …, rn。如果要堆一个雪人,就需要三个半径互不相等的雪球。
例如:
三个雪球的半径为1、2、3,能够用来堆一个雪人。但是半径为2、2、3或者2、2、2的三个雪球就不可以。
快帮帮小陈老师,算算他最多能用这些雪球堆多少个雪人。
Input
对于每组测试数据:
第1行,包含一个整数n(1≤n≤100000) — 雪球的数量。
第2行,包含n个整数 — 雪球的半径r1, r2, …, rn (1≤ri≤1000000000)。
处理到文件结束
Output
对于每组测试数据:
第1行,输出最多能堆多少雪人 - k。
接下来k行,每行描述一个雪人,每行用空格分割三个数字分别表示大雪球、中雪球、小雪球的半径。
可以用任何顺序输出每个雪人。如果有多种可行解,输出任意一个即可。
Sample Input
7
1 2 3 4 5 6 7
3
2 2 3
Sample Output
2
3 2 1
6 5 4
0
Author
齐达拉图
【思路分析】先把所有的雪球排序,然后把每种雪球存到一个优先队列里面,数量多的在前面,然后一直贪心啊,乱搞啊就好了。
【AC代码】
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<stack> using namespace std; struct node { int num,value; }b[100005]; bool operator<(node x,node y) { return x.num<y.num; } bool cmp(int x,int y) { return x>y; } int re[100005][4]; int a[100005]; int main() { int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { scanf("%d",&a[i]); } sort(a,a+n); int cnt=0; b[cnt].num=1,b[cnt++].value=a[0]; priority_queue<node>pq; node tmp; for(int i=1;i<n;i++) { if(a[i]==a[i-1]) { b[cnt-1].num++; } else { tmp.num=b[cnt-1].num; tmp.value=a[i-1]; pq.push(tmp); b[cnt].num=1; b[cnt++].value=a[i]; } } tmp.num=b[cnt-1].num; tmp.value=a[n-1]; pq.push(tmp); /*for(int i=0;i<cnt;i++) { printf("%d %d\n",b[i].num,b[i].value); }*/ //sort(b,b+cnt,cmp); //printf("***\n"); int flag=1,sum=0,x=0; while(pq.size()>2) { //printf("+++\n"); int y=0; node tmp1,tmp2,tmp3; tmp1=pq.top(); pq.pop(); tmp2=pq.top(); pq.pop(); tmp3=pq.top(); pq.pop(); re[sum][y++]=tmp1.value; re[sum][y++]=tmp2.value; re[sum][y++]=tmp3.value; sort(re[sum],re[sum]+3,cmp); sum++; if(--tmp1.num) pq.push(tmp1); if(--tmp2.num) pq.push(tmp2); if(--tmp3.num) pq.push(tmp3); } printf("%d\n",sum); for(int i=0;i<sum;i++) { printf("%d %d %d\n",re[i][0],re[i][1],re[i][2]); } } return 0; }
相关文章推荐
- hrbust 哈理工oj 1176 小陈老师、雪人(优先队列)
- HrbustOJ 1176 小陈老师、雪人 (STL map 和优先队列)
- HLG 1176 小陈老师、雪人【优先队列+map】
- hrbust1176小陈老师、雪人 map&&priority queue
- hrbust 1176 小陈老师、雪人【map+优先队列】
- hbrust 1176 小陈老师、雪人【map+优先队列+栈】
- 哈理工OJ 1045 Draw A Square(简单模拟)
- hrbust 哈理工 oj 1585 公主之魔镜魔镜 (优先队列)
- hrbust 哈理工oj 1020 Robot Motion【简单模拟】
- 哈理工OJ 1031 _OOOO_ Problem(简单模拟问题)
- 哈理工OJ 1569 比赛排名(排序和字符串处理)
- Hrbust 1176 小陈老师、雪人
- 哈理工oj Hrbustacm 1287 数字去重和排序II(STL 或着 hash 拉链法)
- 哈理工OJ 2255 镇守府海域·镇守府近海(模拟题目)
- hrbust/哈理工oj 1605 用户体验之页码输出【模拟】
- hrbust/哈理工oj 1657 数码世界【模拟】
- 哈理工OJ 1150 相识(排序后枚举)
- 哈理工OJ 2111 Apple(简单模拟)
- hrbust 哈理工oj 1330 邂逅【模拟】
- 哈理工OJ 2284 Sign Deliveries(结构体排序)