HDU 5135 Little Zu Chongzhi's Triangles(贪心||状压dp)2014ICPC 广州站现场赛

Little Zu Chongzhi's Triangles

 

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

 

 

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

 

题意:给你N个边的长度,求这些边在不重复使用的情况下能组成的三角形们的最大面积。

分析:

 

(状压)考虑到边的个数比较少,可以用DP来搞搞,我们用二进制来表示当前的边选不选,然后预处理出来所有的情况,dp出最大面积即可。

 

状压:

 

 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include<malloc.h>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf 0x3f3f3f3f
#define sqr(x) (x) * (x)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const int Max=1005;
double cal(double a,double b,double c)
{
    return 0.25*sqrt((a+b+c)*(b+c-a)*(a+c-b)*(a+b-c));
}
double check(double a,double b,double c)
{
    if(a+b>c&&a+c>b&&b+c>a)
        return true;
    return false;
}
vector<int>ans;
double dp[1<<15];
int n;
double A[22];
int main()
{
    while(~scanf("%d",&n)&n)
    {
        memset(dp,0,sizeof dp);
        for(int i=0; i<n; i++)
            scanf("%lf",&A[i]);
        sort(A,A+n);
        int all=(1<<n)-1;
        double save=0;
        for(int i=1; i<=all; i++)
        {
            ans.clear();
            for(int j=0; j<n; j++)
            {
                if(i&(1<<j))
                    ans.push_back(j);
            }
            if(ans.size()<3) continue;
            int m=ans.size();
            for(int a=0; a<m; a++)
                for(int b=a+1; b<m; b++)
                    for(int c=b+1; c<m; c++)
                    {
                        int x=ans[a],y=ans[b],z=ans[c];
                        int tm=i-(1<<x)-(1<<y)-(1<<z);
                        if(check(A[x],A[y],A[z]))
                            dp[i]=max(dp[i],dp[tm]+cal(A[x],A[y],A[z]));
                        save=max(save,dp[i]);
                    }
        }
        printf("%.2lf\n",save);
    }
    return 0;
}


题目链接:点击打开链接

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值