hdu 5131 Little Zu Chongzhi's Triangles【贪心】

来源:互联网 发布:九星照命的解法和算法 编辑:程序博客网 时间:2024/05/21 10:01

Little Zu Chongzhi's Triangles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 1557    Accepted Submission(s): 859

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

3

1 1 20

7

3 4 5 3 4 5 90

0

Sample Output

0.00

13.64

Source

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


题目大意: 

给你N根木棒,告知你这些木棒的长度,让你组成任意个数的三角形,使其面积和最大。


思路:


1、首先将N根木棒按照长度从大到小排序。


2、然后我们想这样一个问题:如果a【i】,a【i+1】,a【i+2】这三根木棒如果组成不了三角形,那么a【i】,a【i+1】,a【i+3】也一定组成不了三角形,因为a【i+2】>a【i+3】,明显如果前三者无法构成三角形,那么将a【i+2】换成了更短的一根木棒明显也组成不了三角形。那么我们按照这个特性,O(n)扫每三根木棒,如果当前三根木棒可以组成三角形,我们就让这三条边组成一个三角形,对应标记上,然后继续扫下一个位子。


3、那么如何证明这样做就能够使得面积最大呢?

我们考虑这样一点:

如果a【i】,a【i+1】,a【i+2】能够组成三角形,那么明显,a【i】,a【i+1】是有可能和a【i+3】组成三角形的,这时候我们为什么选a【i+2】就一定最优呢?

大家不妨拿出两根笔,表示确定下来的a【i】,a【i+1】,再拿出另外几只长度不同的笔,我们搭建出来的三角形面积,明显是第三条边越大越好。


Ac代码:


#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;int a[50];int vis[50];int main(){    int n;    while(~scanf("%d",&n))    {        if(n==0)break;        for(int i=0;i<n;i++)        {            scanf("%d",&a[i]);        }        memset(vis,0,sizeof(vis));        sort(a,a+n);        reverse(a,a+n);        double ans=0;        for(int i=0;i<n;i++)        {            if(vis[i]==1)continue;            if(i+1>=n||i+2>=n)break;            int aa=a[i];            int bb=a[i+1];            int cc=a[i+2];            if(aa+bb>cc&&aa+cc>bb&&bb+cc>aa)            {                double  q=(aa+bb+cc)*1.0/2*1.0;                ans+=sqrt(q*(q-aa)*(q-bb)*(q-cc));                vis[i]=1;vis[i+1]=1;vis[i+2]=1;            }        }        printf("%.2lf\n",ans);    }}





0 0
原创粉丝点击