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开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完 ...
随机推荐
- and or类比c中的 bool?a :b
a = "heaven" b = "hell" c = True and a or b print c d = False and a or b ...
- toLocaleSting()
之前一直忽略了这一方法,直到前天的笔试题,两种方式实现如下功能... 1234567890→1,234,567,890 当时我的思路是这样的:1.字符串反转,插入逗号,再反转 2.求余数,将字符串一分 ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- CSS div 塌陷问题
嵌套塌陷 上下塌陷 overflow:hidden;
- zoj3772Calculate the Function(矩阵+线段树)
链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1 0 这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...
- json字符串和字典类型的相互转换
在开发过程中,有时候需要将json字符串转为字典类型,反之亦然,通常采用.Net的开源类库Newtonsoft.Json进行序列化,这里我也是采用这个,不过我更喜欢写扩展方法方便在项目的调用. 首先新 ...
- linux小白成长之路12————Docker部署Nginx
[内容指引] Docker安装Nginx: 简单启动: 准备配置文件: 一.Docker安装Nginx 指令:docker pull nginx 二.简单启动 指令:docker run --name ...
- 修改他人电脑的Windows登录密码
在别人电脑已登录Windows的情况下: 打开控制面板 -> 管理工具 -> 计算机管理 或者 对Win图标单击右键 -> 计算机管理 -> 本地用户和组 -> 用 ...
- Android RxJava小结
一.如何使用 在build.gradle中添加依赖 dependencies { api 'io.reactivex:rxandroid:1.2.1' api 'io.reactivex:rxjava ...
- 年度精品 XP,32/64位Win7,32/64位Win10系统【电脑城版】
随着Windows 10Build 10074 Insider Preview版发布,有理由相信,Win10离最终RTM阶段已经不远了.看来稍早前传闻的合作伙伴透露微软将在7月底正式发布Win10的消 ...