哈理工OJ 1176 小陈老师、雪人(排序+优先队列模拟)

来源:互联网 发布:电视软件哪个好 编辑:程序博客网 时间:2024/05/17 01:53

题目链接: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代码】

#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;}
0 0
原创粉丝点击