HDOJ 5135 Little Zu Chongzhi's Triangles 狀壓DP,

來源:互聯網
上載者:User

HDOJ 5135 Little Zu Chongzhi's Triangles 狀壓DP,


狀壓DP

Little Zu Chongzhi's TrianglesTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 88    Accepted Submission(s): 49


Problem DescriptionZu 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. 
InputThere 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. 
OutputFor 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
 


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn=30;int n;int a[maxn];int ken[1<<14],kn;double area[1<<14];double dp[1<<14];int main(){    while(scanf("%d",&n)!=EOF&&n)    {        memset(ken,0,sizeof(ken)); kn=0;        memset(area,0,sizeof(area));        memset(dp,0,sizeof(dp));        for(int i=0;i<n;i++)        {            scanf("%d",a+i);        }        sort(a,a+n);        /// mei ju san ge bian        bool flag=false;        for(int i=0;i<n;i++)        {            for(int j=i+1;j<n;j++)            {                for(int k=j+1;k<n;k++)                {                    if(a[i]+a[j]>a[k]&&a[j]+a[k]>a[i]&&a[k]+a[i]>a[j])                    {                        flag=true;                        double A=a[i],B=a[j],C=a[k];                        double p=(A+B+C)/2.;                        double s=sqrt(p*(p-A)*(p-B)*(p-C));                        int id=(1<<i)|(1<<j)|(1<<k);                        ken[kn]=id;                        area[kn++]=s;                    }                }            }        }        if(flag==false)        {            puts("0.00"); continue;        }        double ans=0.0;        for(int i=0;i<(1<<n);i++)        {            for(int j=0;j<kn;j++)            {                if((i&(ken[j]))==0)                {                    int now=i|ken[j];                    dp[now]=max(dp[now],dp[i]+area[j]);                }            }            ans=max(dp[i],ans);        }        printf("%.2lf\n",ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.