您的位置:首页 > 其它

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

2015-09-25 16:47 513 查看


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 <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf 0x3f3f3f3f
#define ll long long int
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
const int mod = 1000000007;
const int maxn = 100005;
int a[maxn];
int check(int a,int b,int c)
{
    if((a+b)>c&&(a+c)>b&&(b+c)>a)
        return 1;
    return 0;
}
double f(int a,int b,int c)
{
    return sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a))/4.0;
}
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n,cmp);
    double sum=0;
    for(int i=0;i<n;i++)
    {
        if(check(a[i],a[i+1],a[i+2]))
            {
                sum+=f(a[i],a[i+1],a[i+2]);
                i=i+2;
            }
        else
           continue;
    }
    printf("%.2lf\n",sum);
    }
    return 0;
}
状压:

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


题目链接:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: