您的位置:首页 > 产品设计 > UI/UE

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

2014-09-23 00:28 435 查看
//利用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
[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stl