HDOJ 5135 Little Zu Chongzhi's Triangles 状压DP

状压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;
}

HDOJ 5135 Little Zu Chongzhi's Triangles 状压DP

时间: 2024-10-26 13:05:50

HDOJ 5135 Little Zu Chongzhi's Triangles 状压DP的相关文章

hdu 5135 Little Zu Chongzhi&#39;s Triangles 状压DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 从大到小贪心是不对的! 比如 4 30 40 50 89 这组数据 结果应该是600 如果取后三条边只会是一个细长细长的三角形根本没多大面积 只不过因为数据水然后贪心碰巧能过而已 看到网上题解很多人用贪心我好郁闷... 状压DP 看到边的数目很少就应该想到这个了 先枚举可能出现的所有三角形 记录他们使用了那些边还有他们的面积 然后再枚举状态之中 套一层遍历所有三角形的循环 如果当前状态和三角形

HDU 5135 Little Zu Chongzhi&#39;s Triangles(状压dp或者贪心)

题目大意:给你n个线段让你任意组成三角形,求组出来的三角形的面积的和最大为多少. 解题思路:首先你得知道海伦公式:S = sqrt(p*(p-a)*(p-b)*(p-c)), p = (a+b+c)/2. 思路一:贪心,按照边的长度进行排序,从大到小判断如果可以构成三角形,就让他构成三角形,这样组成的三角形的面积和一定是最大的. 思路二:状压dp,先暴力求出来所有可以组成的三角形对应的状态和面积,然后dp求解,状态转移公式是:dp[i|(f[j].xnum)] = max(dp[i|(f[j].

HDU 5131 Little Zu Chongzhi&#39;s Triangles (状压DP +2014广州现场赛)

题目链接:HDU 5131 Little Zu Chongzhi's Triangles 题意:给出一些线段,在其中选出3根组成三角形,问用这些线段组成的所有三角形的最大面积是多少. 7 3 4 5 3 4 5 90 两个三角形是(3,3,4),(5,5,4). 思路:N最大12,状态压缩,把所有可能组成的三角形存起来.A&B==0则说明A|B状态是有效的. 贪心也能过..为什么? AC代码: #include <stdio.h> #include <string.h> #

HDU 5135 Little Zu Chongzhi&#39;s Triangles (14广州 状压dp)

Little Zu Chongzhi's Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 474    Accepted Submission(s): 252 Problem Description Zu Chongzhi (429–500) was a prominent Chinese mathematici

hdu 5135 Little Zu Chongzhi&#39;s Triangles

http://acm.hdu.edu.cn/showproblem.php?pid=5135 题意:给你N个木棍的长度,然后让你组成三角形,问你组成的三角形的和最大是多少? 思路:先求出可以组成的所有的三角形,然后状压dp就可以.求所有的三角形也可以用状压,也可以三重循环求. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #define

HDU 5135 Little Zu Chongzhi&#39;s Triangles(简单水题)

题目链接: 戳我 题目大意: 给一堆 木棍,用这些木棍组成三角形,要组成的所有的三角形的面积和最大,不一定要用完所有的木棍. 样例解释: 3 //三个棍子 1 1 20   // 每个棍子的长度,自然,这三个棍子不可能组成三角形,故输出 0.00 7          // 7个棍子 3 4 5 3 4 5 90 // 组成两个三角形(3, 3 4)和(4, 4, 5),面积和即为13.64 0   // 输出 0 退出 解题思路: 本来不想写题解的,因为太水了,,,,,,, 可是看到 谷歌搜出

[HDU 5135] Little Zu Chongzhi&#39;s Triangles (dfs暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边从小到大排序,这样前面的两条边加起来如果不大于第三条边就可以跳出,这是一个存在性条件. dfs(int idx,int now,int cnt,int nowmax)代表我当前处理的是第idx条边,已经加入边集的有cnt条边,当前的边的长度和为now,组成的最大面积和为nowmax. 暴力枚举每个三

hdoj 5125 Little Zu Chongzhi&#39;s Triangles【状态压缩dp】

题目:hdoj 5125 Little Zu Chongzhi's Triangles 题意:给出n个木棍的长度,然后问这些木棍所能组成三角形的最大面积. 分析:这个题目用状态压缩解,因为木棍的最大个数为12 我们枚举所有状态,用状态对应位的 0 和 1 表示这个木棍是否选择,然后如果某个状态选择的木棍是3的话,判断是否可以组成,可以的话dp[st] = 三角形面积 然后大于三的,分解之后得出转移方程dp[st] = max(dp[st],dp[i] + dp[st - i]) (i | (st

HDU 5135 Little Zu Chongzhi&#39;s Triangles(状态压缩dp+Vector)

这道题是水题,当时直接贪心就过了. 多阶段决策,其实应该用dp,他人的代码使用Vector进行预处理. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; int n, a[12]; double dp[1<<12]; double cal(int a, int b, i