Little Zu Chongzhi's Triangles
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 1811 Accepted Submission(s): 1016
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.
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.
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
Source
思路:普通状态压缩dp。
# include <iostream> # include <cstdio> # include <cstring> # include <cmath> using namespace std; double dp[13][1<<13]; int a[13], b[1<<13]; int fun(int n)//计算二进制有多少个1. { int k = 0; while(n) { k += n&1; n >>= 1; } return k; } double fun2(int n)//计算三根棒的面积。 { int k=0, c=1, t[3]; double d; while(n) { if(n&1) t[k++] = a[c]; n >>= 1; ++c; } if(t[0]+t[1] <= t[2] || t[1]+t[2]<=t[0] || t[0]+t[2]<=t[1]) return 0; d = t[0] + t[1] + t[2]; d /= 2; return sqrt(d*(d-t[0])*(d-t[1])*(d-t[2])); } int main() { int n; while(~scanf("%d",&n),n) { for(int i=1; i<=n; ++i) scanf("%d",&a[i]); int up = 1<<n, cnt=0; memset(dp, 0, sizeof(dp)); for(int i=0; i<up; ++i) { int num = fun(i); if(num == 3) { dp[3][i] = fun2(i); b[cnt++] = i; } } for(int i=6; i<=n; i+=3) for(int j=1; j<up; ++j) { if(dp[i-3][j]==0) continue; for(int k=0; k<cnt; ++k) if((j&b[k])==0) dp[i][j|b[k]] = max(dp[i][j|b[k]], dp[i-3][j] + dp[3][b[k]]); } int i; double imax = 0; for(i=n; i%3; --i); for(int j=0; j<up; ++j) imax = max(imax, dp[i][j]); printf("%.2f\n",imax); } return 0; }