POJ 1862

今天的最后一道题了。。
23:59
在POJ上活捉刷题队长一只。
这里写图片描述
中间就隔了仨人

呃呃说正事
题意:
给几个数,合并的代价是 2*sqrt(x1*x2) 问n个数合并的代价最小值。
证明(Discuss里的):
假设有a,b,c 且结果是r
则 r = 2*sqrt(2*sqrt(a*b)*c)
则 r^2/8 = sqrt(a*b*c*c);
若要 r 最小 则 c 一定是 a,b,c 中最小的 所以就是不断地取两个大数相乘喽~~

再说一遍:STL大法好。(其实这道题sort一下也行,当时没有注意到这点)

// by Sirius_Ren
#include <queue>
#include <cmath>
#include <cstdio>
using namespace std;
int main(){
    priority_queue<double>pq;
    register double xx,yy,n;
    scanf("%lf",&n);
    while(n--)scanf("%lf",&xx),pq.push(xx);
    xx=pq.top();pq.pop();
    while(!pq.empty())yy=pq.top(),pq.pop(),xx=2*sqrt(xx*yy);
    printf("%.3lf",xx);
}

这里写图片描述

原文地址:https://www.cnblogs.com/SiriusRen/p/6532461.html