优先队列-结构体的使用

#include<bits/stdc++.h>
using namespace std;

struct node{
	int pos;
	double t;
	bool operator()(node a,node b){
		return a.t >= b.t; //最小值优先 
	}
};

int main(){
	priority_queue< node,vector<node>,node> pq;
	int n;
	double sum=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		node nd;
		nd.pos=i;
		cin>>nd.t;
		pq.push(nd)	;		
	}

	if(n==1){
		node nn=pq.top();
		pq.pop();
		printf("1 %.2lf\n",nn.t);
		return 0;
	}

	while(pq.size()!=1){
		node n1=pq.top();
		pq.pop();
		node n2=pq.top();
		pq.pop();
		sum+=( n1.t+n2.t );
		if(n1.pos!=-1)	cout<<n1.pos<<" ";
		if(n2.pos!=-1)	cout<<n2.pos<<" ";
		node n3;
		n3.pos=-1;
		n3.t=n1.t+n2.t;
		pq.push(n3);
	}
	printf("%.2lf\n",sum/n);
}

  

posted @ 2019-08-20 08:35  bear_ge  阅读(409)  评论(0编辑  收藏  举报