Stripies POJ-1862

题目链接

思路

首先,先将出思路:每次取出较大的数进行合并,最后的结果最小
证明:

假设一共有 \(3\) 个数 \(a,b,c\) 和最后的答案 \(w\)
\(a,b,c\) 进行排列后答案 \(w\) 就可以表示为 \(2 \times \sqrt{2 \times a \times \sqrt{b \times c}}\)
经过等式变换后可得 \(w^2/8 = a^2 \times \sqrt{b * c}\) ,由于 \(a\) 的指数是最大的,所以最后一个合并的数要尽可能的小,所以要从大到小合并。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
int n;
priority_queue <double,vector <double>,less <double> > heap;
int main () {
	cin >> n;
	for (int i = 1;i <= n;i++) {
		int x;
		cin >> x;
		heap.push (x);
	}
	while (heap.size () > 1) {
		double a = heap.top ();heap.pop ();
		double b = heap.top ();heap.pop ();
//		cout << a << ' ' << b << endl;
		double t = 2 * sqrt (a * b);
		heap.push (t);
	}
	printf ("%.3lf",heap.top ());
	return 0;
}
posted @ 2022-09-02 20:40  incra  阅读(19)  评论(0)    收藏  举报