0-1背包-poj-1948-Triangular Pastures

poj-1948-Triangular Pastures

携程大赛2014.4.11 预赛第二场第二题

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. 

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. 

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

5

1

1

3

3

4

Sample Output

692

 大意:用给的几段篱笆围一个牛圈。所有篱笆必须全用上!输出能围成的最大的三角形牛圈。0-1背包DP问题。

代码:优化前后的两组

 

 

时间: 2024-08-04 13:06:35

0-1背包-poj-1948-Triangular Pastures的相关文章

poj 1948 Triangular Pastures(二维0/1背包)

点击打开链接poj 1948 思路: 二维0/1背包 分析: 1 题目要求从n个木棒里面选出m个组成一个三角形,使得三角形的面积最大 2 对于三角形来说知道了两条边和周长就可以求面积,按照0/1背包的思想dp[i][j][k]表示前i个木棒能否组成第一条边为长度j,第二条长度为k.如果dp[j-v[i]][k] 或dp[j][k-v[i]] 为true则dp[j][k]就为true 3 我们求出了所有可能的组合之和,就去枚举所有的边长的情况,然后求最大的面积 代码: #include<cmath

算法:poj 1948 Triangular Pastures (dp 二维01背包)

题目大意: 给N条边,把这些边组成一个三角形,问面积最大是多少?必须把所有边都用上. 思路: 对于已知周长的三角形,我们只要知道两条边的长度变可推出第三条边,所以可以得 到状态方程: f[i][j][k] 表示用前i条边,能否组成长度为j和k的两条边 初始化f[0][0][0] = true: f[i][j][k] = f[i-1][j-len[i]][k] || f[i-1][j][k-len[i]]; 如果f[i][j][k]=true ,那么就计算以j,k,sum-j-k为三条边能否组成三

poj 1976 A Mini Locomotive(0/1背包)

点击打开链接poj 1976 思路: 0/1背包 分析: 1 题目给定n个数,要求三段连续的m个数之和最大 2 假设n个数是num[1],num[2],num[3]......num[n],那么m个连续的数可能有n-m+1种可能即num[1]+num[2]...+num[k] , num[2]+num[3]...+num[k]...... 3 我们令v[1] = num[1]+num[2]...+num[k] , v[2] = num[2]+num[3]...+num[k].那么总的有n-m+1

poj 3211 Washing Clothes(0/1背包)

点击打开链接poj 3211 思路: 0/1背包 分析: 1 题目要求洗完n件衣服所需的最少的时间,并且必须洗完一种颜色才能跳到下一种 2 仔细想想这一题和uva上面的一道分金币非常的相似,由于要求必须洗完一种颜色才能跳到下一种,那么我们只要使得洗每一种颜色的时间最少那么总的就最少,那怎样才能够最少的时间呢?也就是要求两个人的时间差最小(也就相当于一个人用一半的时间去能够洗的最多的衣服),那么就转化为0/1背包的思想 3 做m次的dp然后求和即可 代码: #include<cstdio> #i

poj 3624 Charm Bracelet (0/1背包)

点击打开链接 裸0/1背包 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAXN = 15000; int n , m; int w[MAXN] , v[MAXN] , dp[MAXN]; int main(){ while(scanf("%d%d" , &n , &

hdu 2955 Robberies(0/1背包)

点击打开链接hdu 2955 思路: 0/1背包 分析: 1 按照题目的意思我们很容易知道这就是一个0/1背包问题,如果我们把概率当作是背包的容量,那么我们遇到一个问题就是浮点数的dp,因为题目没有告诉我们小数点具体几位,那么我们就不能够通过乘上10^n来转化为整数,所以我们应该考虑换种思想 2 按照正常的思路是dp[i][j]表示前i个物品放入概率为j的最大价值,那么我们这边把价值当成背包来看的话我们设dp[i][j]表示前i个物品放入金钱为j的最小被抓概率(因为题目不好求最小概率我们可以认为

hdu 3466 Proud Merchants(0/1背包)

点击打开链接hdu 3466 思路: 0/1背包 分析: 1 这一题和"hdu2546 饭卡"有点像,但是又有不同,不同的是这一题每一个物品都有一个限制的值Q[i],求最大的价值 2 题目明确提到每一种物品只能卖一次或这不卖,那这明显就是0/1的性质,但是现在多了一个条件钱不能少于Q[i].这边的话我各种YY无果之后,果断看了题解,发现是要按照q-p排序,然后再做dp. 3 经过一番的YY之后,我明白了为什么按照q-p是正确的.我们都知道dp有一个很重要的特点就是无后效性,如果没有金钱

hdu 2639 Bone Collector II (0/1背包)

点击打开链接hdu 2639 思路: 0/1背包求第k优解 分析: 1 其基本思想是,将每个状态都表示成有序队列,将状态转移方程中的 max/min 转 化成有序队列的合并. 2 这里仍然以 01 背包为例讲解一下.    首先看 01 背包求最优解的状态转移方程: F [i, v] = max{F [i − 1, v], F [i − 1, v −Ci ] + Wi }.如果要求第 K 优解,那么状态 F [i, v] 就应该是一个大小为 K 的队列F [i, v, 1 . . . K].其中

uva 562 Dividing coins (0/1背包)

点击打开链接uva 562 思路: 0/1背包 分析: 1 题目意思是有两个人分n个金币,要求最后两个人的金币差值最小 2 我们利用背包的思想即背包的总容量为金币总和的一半,去求出可以放入背包的最大的金币(这里其实就是某一个人能获得的最大的金币),那么最后的ans就是(sum-dp[sum/2])-dp[sum/2] 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorit

hdu 2126 Buy the souvenirs(二维0/1背包)

点击打开链接hdu2126 思路: 二维0/1背包 分析: 1 题目给定n个物品的价钱和m的钱,问最多能买到的物品数有几种方案. 2 很明显就可以写出状态转移方程dp[i][j][k]表示的是前i个物品选j个总价钱为k的方案数 那么dp[i][j][k] = dp[i-1][j][k]+dp[i-1][j-1][k-v[i]].由于都可以把第一维去掉,所以正常的情况下直接写出dp[j][k] = dp[j][k] + dp[j-1][k-v[i]] 代码: #include<cstdio> #