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

来源:互联网 发布:淘宝销量查询网站 编辑:程序博客网 时间:2024/05/16 01:15
//利用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