现在的位置: 首页 > 综合 > 正文

HDOJ 5135 Little Zu Chongzhi’s Triangles 状压DP

2017年11月23日 ⁄ 综合 ⁄ 共 2561字 ⁄ 字号 评论关闭

状压DP

Little Zu Chongzhi's Triangles

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


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
 

#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;
}

抱歉!评论已关闭.