Little Zu Chongzhi's Triangles(状态压缩dp)

Little Zu Chongzhi's Triangles

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


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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
struct triangle{
    ll id;
    double area;
};
int n;
vector<triangle>v;
int c[maxn];
double dp[maxn*100];
int main() {
    while(~scanf("%d",&n)&&n){
        for(register int i=1;i<=n;++i){
            scanf("%d",c+i);
        }
        sort(c+1,c+1+n);
        v.clear();
        for(register int i=1;i<=n;++i){
            for(register int j=i+1;j<=n;++j){
                for(register int k=j+1;k<=n;++k){
                    if(c[k]<c[i]+c[j]&&c[j]-c[i]<c[k]){
                        double p=0.5*(c[i]+c[j]+c[k]);
                        ll pos=(1<<i)|(1<<j)|(1<<k);
                        double area=sqrt(p*(p-c[i])*(p-c[j])*(p-c[k]));
                        v.emplace_back(triangle{pos,area});
                    }
                }
            }
        }
        for(register int i=1;i<(1<<(n+1));++i){
            dp[i]=0;
        }
        for(register int i=1;i<(1<<(n+1));++i){
            for(register int j=0;j<v.size();++j){
                int pos=v[j].id;
                if((i&pos)==0){
                    dp[i|pos]=max(dp[i|pos],dp[i]+v[j].area);
                }
            }
        }
        double ans=0.0;
        for(register int i=1;i<(1<<(n+1));++i){
            ans=max(ans,dp[i]);
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/czy-power/p/11248511.html