hrbust1176小陈老师、雪人 map&&priority queue

//利用map的去重性存储,很好;再用优先队列排序
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;
const int N=100009;
map<int,int> m;
struct node{
    int r,n;
    friend bool operator <(const node &a,const node &b){//优先队列只用<,>重载了也没用
        return a.n<b.n;
    }
};
priority_queue<node> q;
int n,tmp;
int main(){
    while(scanf("%d",&n)!=EOF){
        m.clear();
        while(n--){
            scanf("%d",&tmp);
            m[tmp]++;
        }
        while(!q.empty())q.pop();
        node test;
        for(map<int,int>::iterator i=m.begin();i!=m.end();i++){
            test.n=(*i).second;
            test.r=(*i).first;
            //cout<<test.n<<' '<<test.r<<endl;
            q.push(test);
        }
        int ans[N][3],cnt=0;
        while(true){
            if(q.empty())break;
            node t1,t2,t3;
            t1=q.top();q.pop();
            cout<<t1.n<<' '<<t1.r<<endl;
            if(q.empty())break;
            t2=q.top();q.pop();
            cout<<t2.n<<' '<<t2.r<<endl;
            if(q.empty())break;
            t3=q.top();q.pop();
            cout<<t3.n<<' '<<t3.r<<endl;
            if(t1.r<t2.r)swap(t1,t2);
            if(t1.r<t3.r)swap(t1,t3);
            if(t2.r<t3.r)swap(t3,t2);
            ans[cnt][0]=t1.r;ans[cnt][1]=t2.r;ans[cnt++][2]=t3.r;
            if(--(t1.n)>0)q.push(t1);
            if(--(t2.n)>0)q.push(t2);
            if(--(t3.n)>0)q.push(t3);
        }
        cout<<cnt<<endl;
        for(int i=0;i<cnt;i++)
            printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值