POJ1948:Triangular Pastures

来源:互联网 发布:淮南大数据公司老总 编辑:程序博客网 时间:2024/06/06 03:05

点击打开题目链接


Triangular Pastures

Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 5919 Accepted: 1901

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

511334

Sample Output

692

Hint

[which is 100x the area of an equilateral triangle with side length 4] 

Source

USACO 2002 February

=====================================题目大意=====================================


用给出的N个篱笆围成三角形,求三角形的最大面积可以是多少(答案乘以100后再取整数部分输出)。


=====================================算法分析=====================================


用F[i][j][k]表示是否可以用前i个篱笆组成两条长度分别为j和k的边,状态转移方程为:

F[i][j][k]=(F[i-1][j-Fence[i]][k]||F[i-1][j][k-Fence[i]])。

因为F[i][j][k]和F[i][k][j]等价,所以可另j>=k,由于三角形的任意一条边小于周长的一半,若周长为sum,则有k<=j<(sum/2)。


=======================================代码=======================================




#include<math.h>#include<stdio.h> #include<string.h>int N,Fence[45];bool F[805][805];int inline IntMax(int A,int B){return A>B?A:B;}bool inline ConsTrig(int A,int B,int C){return (A+B>C)&&(A+C>B)&&(B+C>A);}int inline TrigArea(int A,int B,int C){double p=(A+B+C)/2.00;double s=sqrt(p*(p-A)*(p-B)*(p-C));return (int)(s*100);}int main(){while(scanf("%d",&N)==1){//读取数据int sum=0;for(int i=0;i<N;++i){scanf("%d",&Fence[i]);sum+=Fence[i];}int half=((sum+1)>>1);//背包DPmemset(F,0,sizeof(F));F[0][0]=1;for(int i=0;i<N;++i){for(int j=half;j>=0;--j){for(int k=j;k>=0;--k) {F[j][k]=((F[j][k])||(j>=Fence[i]&&F[j-Fence[i]][k]));F[j][k]=((F[j][k])||(k>=Fence[i]&&F[j][k-Fence[i]]));}}}//获取答案int ans=-1;for(int i=1;i<=half;++i){for(int j=half-i+1;j<=i;++j) {int k=sum-i-j;if(F[i][j]&&ConsTrig(i,j,k)) {ans=IntMax(ans,TrigArea(i,j,k));}}}printf("%d\n",ans);}  return 0;}

原创粉丝点击