贪心 哈夫曼思想

贪心 哈夫曼思想

题意:科学家发现一种奇怪的玩意,他们重量分别是Wi,
如果他们碰在一起,总重变成2*sqrt(W1 * W2)。
要求出最终的重量的最小值。

思路:哈夫曼思想,把所有重量放优先队列里,这里每次取俩最大的处理后再放回去,最后队列剩下的最后元素就是最小的

需要注意的是oj交G++的时候,double型的输出格式用%f,而不是%lf。C++可以过
更加详细可以这篇博文G++和C++区别和评测注意事项

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
priority_queue<double,vector<double>,less<double> >w; //优先队列
int main() 
{
	int n;
	double tmp;
	cin>>n;
	while(n--){
		scanf("%lf",&tmp);
		w.push(tmp);
	}
	double a,b;
	while(w.size()>1){
		a=w.top();
		w.pop();
		b=w.top();
		w.pop();
		w.push(2.0*sqrt(a*b));
	}
	//cout.precision(3);//
	//cout<<w.top()<<endl;//
	printf("%.3f\n",w.top());//输出方式
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jianglw1/article/details/83037103