POJ1948:Triangular Pastures

点击打开题目链接


Triangular Pastures

Time Limit: 1000MS Memory Limit: 30000K
Total 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

5
1
1
3
3
4

Sample Output

692

Hint

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

Source


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


用给出的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);
		//背包DP
		memset(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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值