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开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完 ...
随机推荐
- 初探Hibernate3源码--读取配置文件
依照个人推测,要实现ORM框架,关键是如何拼凑sql语句再通过jdbc来进行对数据库的访问,另外将数据库返回的数据记录封装成对应的类也是必不可少的. 那么实现ORM框架的基本思路如下: 1,读取xml ...
- python_面向对象进阶(7)
第1章 面向对象特性—继承(补充) 1.1 接口类.抽象类介绍 1.2 接口类 1.3 接口类应用过程 1.3.1 第一版:完成多种支付方式接口 1.3.2 第二版: 归一化设计,统一支付方式 1.3 ...
- RHEL 6.5----SCSI存储
主机名 IP master 192.168.30.130 node-1 192.168.30.131 node-2 192.168.30.132 安装并启动 [root@master ~]# ll / ...
- solr的多条件组合查询和solr的范围查询【转】
solr的多条件组合查询和solr的范围查询 版权声明:本文为博主原创文章,供大家参考,但不要抄袭哦! 存在问题:为了减轻数据库的访问压力,往往我们将必要的数据存储到solr中,并给部分字段建立索引, ...
- Suricata里的规则与Snort区别之处
不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/rules/differences-from-snort.html
- phpcms v9模板制作教程
phpcms v9模板制作教程(转载) 第一节 1.首先下载phpcms v9的集成安装包并安装,这里就不详细说明了. 2.本地调试建议大家使用APMserver,或者wampserver等,可以到P ...
- Android提供的对话框
1.普通对话框: 给出提示信息,有yes.no两个按钮. AlertDialog dialog=new AlertDialog.Builder(this) //this代表当前Activity对象,表 ...
- 伟景行 citymaker 从入门到精通(1)——js开发,最基本demo,加载cep工程文件
开发环境:citymaker 7(以下简称cm),jquery,easyui 1.4(界面),visual studio 2012(没有vs,不部署到IIS也行,html文件在本地目录双击打开可用) ...
- 你的项目刚刚启动?是时候考虑Globalization了!
今天继续由SAP成都研究院非典型程序猿, 菜园子小哥王聪给大家带来分享. 关于这个很长的定语的由来,请参考这篇文章,里面有王聪的背景介绍,包括他种菜的特长:当我用UI5诊断工具时我用些什么. 秋天到了 ...
- (转)编码剖析Spring管理Bean的原理
http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...