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.
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
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;}
- poj1948 Triangular Pastures
- POJ1948:Triangular Pastures
- dp poj1948 Triangular Pastures
- POJ1948 Triangular Pastures(DP)
- POJ1948 Triangular Pastures(01背包)
- poj1948 Triangular Pastures(背包)
- 【POJ1948】Triangular Pastures(背包)
- poj1948[USACO2002,Feb]Triangular Pastures三角牧场
- [POJ1948]Triangular Pastures(背包dp)
- POJ1948:Triangular Pastures(二维01背包)
- Triangular Pastures
- poj 1948 Triangular Pastures
- poj 1948 Triangular Pastures
- POJ 1948 Triangular Pastures
- poj_1978 Triangular Pastures
- poj 1948 Triangular Pastures
- poj 1948 Triangular Pastures
- poj 1948 Triangular Pastures
- 关于SNMP的MIB文件的语法简述
- 异步加载缓存图片生成缩略图lib
- 巨高兴,偶的文章 “如何在服务器上配置ODBC来访问本机DB2for Windows服务器”被推荐至CSDN博客首页
- SQL优化查询详解
- 整理QQ空间,摘出来的小知识
- POJ1948:Triangular Pastures
- 不同语言字节转换
- 集合中取集合的值
- Ubuntu顯示networking disabled的解決辦法
- Hibernate笔记03Hibernate中关系映射文件中基本属性的配置
- HTTP协议详解(真的很经典)
- 沿途的风景
- 计算机程序的构造与解释作业:练习1.16到1.19
- Eclipse找不到Further configuration available…