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开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完 ...
随机推荐
- Hibernate的一级缓存:快照区
参考来源:http://blog.sina.com.cn/s/blog_981ee5d80102w85f.html
- canvas绘图出现模糊,解决方法
在项目开发中发现,canvas有一个问题,绘制的图会出现模糊现象. 解决方法之一:将canvas元素放大2倍,然后将整个canvas元素或者其父元素缩小两倍. <!DOCTYPE html> ...
- jquery选择器 选择除当前点击元素外所有的元素
问题: 多个select选择,如果已选择某些value ,该value不可再选 思路: 点击当前元素,js列出除当前元素外所有的元素 当前解决办法: function symbolDefine(ob ...
- AJPFX关于学习java遇到的问题:对算法和数据结构不熟悉
为什么我先拿“数据结构和算法”说事捏?这玩意是写程序最最基本的东东.不管你使用 Java 还是其它的什么语言,都离不开它.而且这玩意是跨语言的,学好之后不管在哪门语言中都能用得上. 既然“数据结构和算 ...
- 通过流传入excel解析的问题
做了个excel文件导入的功能,接收excel文件流,先读取文件流头部信息确定文件类型,然后调用poi方法 OPCPackage pkg = OPCPackage.open(inputStream) ...
- Math.net,.net上的科学计算利器
F#在科学计算领域的应用,包括部分语法介绍. Math.net,.net上的科学计算利器 摘要: .net上科学计算个人觉得首选numpy和scipy for dotnet.因为这两个库用户数量已经非 ...
- iOS开发之cell位置contentOffset的用法
@property(nonatomic) CGPoint contentOffset; // default ...
- FPGA编程技巧系列之按键边沿检测
抖动的产生: 通常的按键所用开关为机械弹性开关,当机械触点断开.闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一下子断开.因而在闭合及断开的瞬间均伴随有一连串的 ...
- COGS 2111. [NOIP2015普及]扫雷游戏
★ 输入文件:2015mine.in 输出文件:2015mine.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 扫雷游戏是一款十分经典的单机小游戏.在 n 行 ...
- SQLite -分离数据库
SQLite -分离数据库 SQLite分离DTABASE语句用于分离和分离命名数据库从一个数据库连接之前附加使用附加语句.如果相同的数据库文件已附加多个别名,然后分离命令将断开只有名字和其他依附仍将 ...