Triangular Pastures (二维01背包)
描述
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.
输入
* 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.
输出
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.
样例输入
5
1
1
3
3
4
样例输出
692
提示
which is 100x the area of an equilateral triangle with side length 4
题目大意:
给定几条边,用上全部的边组成三角形,输出最大的三角形面积,若不存在,输出-1.
大体思路:
枚举所有可能出现的边的情况,在判断是否能成为三角形,然后进行处理
#include <bits/stdc++.h>
using namespace std;
const int N=;///40*40
int dp[N][N]={};///dp[i][j]代表能否构成i,j两边
int a[];
int area(int a,int b,int c)
{
double p=(a+b+c)/2.0;
return (int)*sqrt(p*(p-a)*(p-b)*(p-c));
}
int main()
{
int n,sum=;
cin>>n;
for(int i=;i<n;i++)
cin>>a[i],sum+=a[i];
dp[][]=;
for(int i=;i<n;i++)
for(int j=sum;j>=;j--)
for(int k=j;k>=;k--)
if(j>=a[i]&&dp[j-a[i]][k]||k>=a[i]&&dp[j][k-a[i]])
dp[j][k]=;
int maxs=-;
for(int i=sum;i>;i--)
for(int j=i;j>;j--)
{
if(dp[i][j])
{
int k=sum-i-j;
if(i<j+k||i+j>k)///判断三边是否组成三角形
maxs=max(maxs,area(i,j,k));
}
}
if(maxs==-) cout<<"-1"<<'\n';
else cout<<maxs<<'\n';
return ;
}
Triangular Pastures (二维01背包)的更多相关文章
- hdu3496 二维01背包
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...
- hdu 2126 Buy the souvenirs 二维01背包方案总数
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Leetcode_474. 一和零(二维01背包)
每个字符串看成一个物品,两个属性是0和1的个数,转换为01背包. code class Solution { public: int w[605][2]; int dp[105][105]; int ...
- POJ 1948 Triangular Pastures【二维01背包】
题意:给出n条边,用这n条边构成一个三角形,求三角形的最大面积. 先求面积,用海伦公式,s=sqrt(p*(p-a)*(p-b)*(p-c)),其中a,b,c分别为三角形的三条边,p为三角形的半周长, ...
- poj 1948二维01背包
题意:给出不多于40个小棍的长度,求出用所有小棍组成的三角形的最大面积. 思路:三角形3边求面积,海伦公式:p=(a+b+c)/2;S=p*(p-a)*(p-b)*(p-c);因为最大周长为1600 ...
- poj3260 平衡问题(二维01背包)
http://www.cnblogs.com/ziyi--caolu/p/3228090.html http://blog.csdn.net/lyy289065406/article/details/ ...
- POJ - 1948 二维01背包
T了两发,DP方程很简单粗暴 dp[i][j][k]:用前i物品使得容量分别为j和k的背包恰好装满 背包的调用只需一次即可,第一次T就是每次check都丧心病狂地背包一次 对于sum的枚举,其实i j ...
- HDU--2126 Buy the souvenirs(二维01背包)
题目http://acm.hdu.edu.cn/showproblem.php?pid=2126 分析:有两个要求,一是计算最多可以选多少中纪念品:而是计算选最多纪念品的方案有多少种, 即统计最优方案 ...
- HDU-2159FATE(二维完全背包)
FATE Problem Description 最 近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完 ...
随机推荐
- 谷歌编码风格内容,新建一个xml文件,复制进去就可以在eclipse里面用了,命名--eclipse-java-google-style.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <profi ...
- archive log full ora-00257
############# sample 0 asmcmd show free 37G in archive_log ASMCMD> lsdgState Type Rebal Unbal Sec ...
- getAttribute()方法的第二个参数
对于一个img元素,我们想获取它的src属性时可以有两种方式: 1.xxx.getAttribute("src") 2.直接通过xxx.src获取属性值 在src的属性值为相对路径 ...
- spark block读写流程分析
之前分析了spark任务提交以及计算的流程,本文将分析在计算过程中数据的读写过程.我们知道:spark抽象出了RDD,在物理上RDD通常由多个Partition组成,一个partition对应一个bl ...
- qconbeijing2016
http://2016.qconbeijing.com/schedule 大会日程 2016年04月21日 星期四 09:15 开场致辞 地点 1号厅 主题演讲 工程效率提升 业务核心架构 容器集 ...
- LINUX 文件夹打包
tar -zcvf /data/www.tar.gz data/www tar -zcvf 打包后生成的文件名全路径 要打包的目录 压缩: 压缩当前的文件夹 zip -r ./xahot.zip ./ ...
- C#实现为类和函数代码自动添加版权注释信息的方法
这篇文章主要介绍了C#实现为类和函数代码自动添加版权注释信息的方法,主要涉及安装文件的修改及函数注释模板的修改,需要的朋友可以参考下 本文实例讲述了C#实现为类和函数代码自动添加版权注释信息的方法 ...
- 开启apahce的mod_speling.so模块,让使用apahce http服务器不再有大小写烦恼
今天把服务器重新安装系统,做apache调优前,优化下apache对网络地址大小写不区分的支持.记录如下: 编译mod_speling.so模块去除Apache-url大小写字母敏感的配置 1. 进入 ...
- js里面Object的一些方法
1.Object.freeze() 阻止修改现有属性的特性和值,并阻止添加新属性两种用法:Object.freeze( { } ) 和 Object.freeze( object ) <scri ...
- makefile vpath变量
在讲vpath之前,我们首先了解以下makefile文件. 在类Unix系统中,当我们使用源码编译某个软件的时候,我们会使用confiure,make,make install这三个命令,其中cofi ...