http://poj.org/problem?id=1948
Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N (3 <= N <= 40) fence segments (each of integer length Li (1 <= Li <= 40) and must arrange them into a triangular pasture with the largest grazing area. Ms. Hei must use all the rails to create three sides of non-zero length.
Help Ms. Hei convince the rest of the herd that plenty of grazing land will be available.Calculate the largest area that may be enclosed with a supplied set of fence segments.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N (3 <= N <= 40) fence segments (each of integer length Li (1 <= Li <= 40) and must arrange them into a triangular pasture with the largest grazing area. Ms. Hei must use all the rails to create three sides of non-zero length.
Help Ms. Hei convince the rest of the herd that plenty of grazing land will be available.Calculate the largest area that may be enclosed with a supplied set of fence segments.
Input
* Line 1: A single integer N
* Lines 2..N+1: N lines, each with a single integer representing one fence segment's length. The lengths are not necessarily unique.
* Lines 2..N+1: N lines, each with a single integer representing one fence segment's length. The lengths are not necessarily unique.
Output
A single line with the integer that is the truncated integer representation of the largest possible enclosed area multiplied by 100. Output -1 if no triangle of positive area may be constructed.
Sample Input
5 1 1 3 3 4
Sample Output
692
/** 题目大意;给定一些长度不等的木棍,求有这些木棍能拼出的三角形的最大面积是多少? 解题思路:二维01背包问题。dp[i][j]表示有两条边为i和j的三角形,用背包列出所有可行的状态,然后比较面积取最大。 由于三角形的边长小于周长的一半,我们从sum/2开始枚举,这样就将时间复杂度降低到1s内可求的范围。 */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int a[45],dp[900][900]; int n; int main() { while(~scanf("%d",&n)) { int sum=0; for(int i=0; i<n; i++) { scanf("%d",&a[i]); sum+=a[i]; } memset(dp,0,sizeof(dp)); dp[0][0]=1; int half=sum>>1; for(int i=0; i<n; i++) { for(int j=half; j>=0; j--) { for(int k=j; k>=0; k--) { if(j>=a[i]&&dp[j-a[i]][k]||dp[j][k-a[i]]&&k>=a[i]) { dp[j][k]=1; } } } } int ans=-1;//这里初始化为0就是错,为什么?? for(int i = half; i >= 1; i--) { for(int j = i; j >= 1; j--) { if(dp[i][j]) { int k=sum-i-j; if(i+j>k&&i+k>j&&j+k>i) { double p = (i + j + k) * 1.0 / 2; int temp = (int)(sqrt(p * (p - i) * (p - j) * (p - k)) * 100); if (temp > ans) ans = temp; } } } } printf("%d\n",ans); } return 0; }