poj 1862 Stripies

题意:

有一种名为stripie的生物,每次当两个质量为m1 和 m2的这种生物相遇之后,它们会合体成为一个个体,这个个体的质量变为2 * sqrt(m1 * m2)。给出n个生物的质量,问他们相遇之后最后剩下一个个体时,最小的质量是多少,保证不会有超过2个这种生物碰到一起的情况。

思路:

哈夫曼树变种,优先队列解决。

代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;
 
priority_queue<double,vector<double>,less<double> > pq;
 
int main()
{
	int n;
	
	while (scanf("%d",&n) != EOF)
	{
		while (!pq.empty()) pq.pop();
		
		for (int i = 0;i < n;i++)
		{
			double x;
			scanf("%lf",&x);
			pq.push(x);
		}
		
		while (pq.size() >= 2)
		{
			double x = pq.top();pq.pop();
			double y = pq.top();pq.pop();
			
			double tmp = 2 * sqrt(x * y);
			
			pq.push(tmp);
		}
		
		printf("%.3f\n",pq.top());
	}
	
	return 0;
}

 

posted @ 2018-04-12 15:33  qrfkickit  阅读(190)  评论(0编辑  收藏  举报