[ACM] HDU 5135 Little Zu Chongzhi's Triangles (一堆木棍组成三角形最大面积)

来源:互联网 发布:多益网络是什么 编辑:程序博客网 时间:2024/04/27 16:54

Little Zu Chongzhi's Triangles



Problem Description
Zu Chongzhi (429–500) was a prominent Chinese mathematician and astronomer during the Liu Song and Southern Qi Dynasties. Zu calculated the value ofπ to the precision of six decimal places and for a thousand years thereafter no subsequent mathematician computed a value this precise. Zu calculated one year as 365.24281481 days, which is very close to 365.24219878 days as we know today. He also worked on deducing the formula for the volume of a sphere. 

It is said in some legend story books that when Zu was a little boy, he liked mathematical games. One day, his father gave him some wood sticks as toys. Zu Chongzhi found a interesting problem using them. He wanted to make some triangles by those sticks, and he wanted the total area of all triangles he made to be as large as possible. The rules were :

1) A triangle could only consist of 3 sticks.
2) A triangle's vertexes must be end points of sticks. A triangle's vertex couldn't be in the middle of a stick.
3) Zu didn't have to use all sticks.

Unfortunately, Zu didn't solve that problem because it was an algorithm problem rather than a mathematical problem. You can't solve that problem without a computer if there are too many sticks. So please bring your computer and go back to Zu's time to help him so that maybe you can change the history.
 

Input
There are no more than 10 test cases. For each case:

The first line is an integer N(3 <= N<= 12), indicating the number of sticks Zu Chongzhi had got. The second line contains N integers, meaning the length of N sticks. The length of a stick is no more than 100. The input ends with N = 0.
 

Output
For each test case, output the maximum total area of triangles Zu could make. Round the result to 2 digits after decimal point. If Zu couldn't make any triangle, print 0.00 .
 

Sample Input
31 1 2073 4 5 3 4 5 900
 

Sample Output
0.0013.64
 

Source
2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大)


解题思路:

题意为给定n个木棍,每个木棍的长度为len[i], 要用这n个木棍组成一些三角形,使得获得的三角形面积最大,求这个最大面积,注意组成的每个三角形只能用三根木棍。

看到网上O(n)的做法,就是先把n个木棍从小到大排好序,然后从后向前扫描,看连续的三个木棍能不能组成三角形,如果可以,则答案加上该面积,如果不可以,就把最大的木棍舍弃,因为该木棍不可能再和其他两根木棍组成三角形(因为是排好序的,其他两根的和一定小于该根),再向前扫描....最后求出的答案即为最终答案。

如果三角形的三条边差异越小,面积越大,比如等边三角形面积是最大的...但该题目的做法正确性没有严格的证明,求指导。

代码:

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <cmath>#include <time.h>#include <iomanip>#include <cctype>using namespace std;#define ll long longbool ok(int a,int b,int c){    return (a+b>c&&a+c>b&&b+c>a);}double area(int a,int b,int c){    double p=(a+b+c)*0.5;    return sqrt(p*(p-a)*(p-b)*(p-c));}int len[14];int n;int main(){    while(scanf("%d",&n)!=EOF&&n)    {        for(int i=1;i<=n;i++)            scanf("%d",&len[i]);        sort(len+1,len+1+n);        double ans=0;        for(int i=n;i>=3;)        {            if(ok(len[i],len[i-1],len[i-2]))            {                ans+=area(len[i],len[i-1],len[i-2]);                i-=3;            }            else                i--;        }        printf("%.2lf\n",ans);    }    return 0;}



0 0