EOJ:Triangular Pastures

Triangular Pastures

Time Limit: 1500MS Memory Limit: 45000K
Case Time Limit: 1000MS
Total Submits: 31 Accepted: 5

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. 

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.

 

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

 

Hint

[which is 100x the area of an equilateral triangle with side length 4]
_______________________________________________________________________________________
题目意思很简单,给你一些木棒,问用这些木棒能够拼出面积最大的三角形的面积(木棒必须都用到)
显然最简单的方法是枚举……当然超时的厉害。
看了题解后发现居然是个背包问题!居然没有想到!
DP[I][J][K]表示前I根木棒能否组成长度为J,K和SUM-J-K的三条边。
最后遍历下数组,算算最大的面积就可以了
1 #include<stdio.h>
2 #include<memory.h>
3 #include<math.h>
4  int f[50], sum[50];
5  bool dp[50][200][200];
6  double l, temp, ans;
7  int i, j, k, n;
8 int main()
9 {
10 ans = 0;
11 memset(dp, 0, sizeof(dp));
12 memset(sum, 0, sizeof(sum));
13 scanf("%d", &n);
14 for (i = 1; i <= n; i++)
15 {
16 scanf("%d", &f[i]);
17 sum[i] = sum[i - 1] + f[i];
18 }
19 l = double(sum[n]) / 2;
20 dp[0][0][0] = true;
21 for (i = 0; i <= n; i++)
22 for (j = 0; j <= sum[i]; j++)
23 {
24 if (j>l) break;
25 for (k = 0; k <= sum[i]; k++)
26 {
27 if (k>l) break;
28 if (dp[i][j][k])
29 {
30
31 if ((l - j) > 0 && (l - k) > 0 && (j + k - l) > 0)
32 {
33 temp = sqrt(l * (l - j) * (l - k) * (j + k - l));
34 if (ans < temp)
35 ans = temp;
36 }
37 dp[i + 1][j][k] = true;
38 dp[i + 1][j + f[i + 1]][k] = true;
39 dp[i + 1][j][k + f[i + 1]] = true;
40 }
41 }
42 }
43 k = floor(ans * 100);
44 if (k == 0)
45 printf("-1\n");
46 else
47 printf("%d\n", k);
48 return 0;
49 }
posted on 2011-03-03 20:20  风也轻云也淡  阅读(312)  评论(0编辑  收藏  举报