POJ 1862 Stripies(优先队列)

#pragma warning(disable:4996)
#include <cstdio>
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;

//思路:
//每次都去最大的两个碰撞,最后得到的一定最小
//证明:
//假设有a,b,c,答案是d,,那么d=2*sqrt(2*sqrt(a*b)*c)
//d*d=8*sqrt(a*b*c*c)可知c的贡献比a和b要多
//推断出,先碰撞的贡献少,后碰撞 的贡献多,,
//所以要从大向小碰撞

int main(){
	int n;
	while (scanf("%d", &n) != EOF){
		priority_queue<double>q;
		int tmp;
		for (int i = 1; i <= n; i++){
			scanf("%d", &tmp);
			q.push((double)tmp);
		}
		for (int i = 1; i <= n; i++){
			if (i == n){
				printf("%.3f\n", q.top());
				q.pop();
				break;
			}
			double a = q.top();
			q.pop();
			double b = q.top();
			q.pop();
			q.push(2.0*sqrt(a*b));
		}
	}
	return 0;
}

你可能感兴趣的:(POJ 1862 Stripies(优先队列))