题目地址:http://poj.org/problem?id=1948 题目大意: 给N条边,把这些边组成一个三角形,问面积最大是多少?必须把所有边都用上. 解题思路: 根据题意周长c已知,求组合三边长使得三角形面积最大.如果直接DFS的话,每次考虑每根木棍放在哪一条边上,爆搜,可以加上每条边不会大于周长的一半的剪枝.根据数据规模,仍会超时,所以可以考虑采用动态规划.因为周长c已知,所以只需考虑其中两条边即可.类似二维0-1背包的做法,开一个二维判定数组f[i][j],i代表第一条边长为i,j为…
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M个光盘,不能多也不能少,所以就要求把背包装满. 恰好把背包装满,那么在初始化时,除了dp[0]=0,剩下的dp[1~M],均为负无穷(其实设置成-1,到时候在判断一下也是一样的,思想相同) 这样才可以保证最终得到的dp[M]是一种恰好装满背包状态的最优解. 代码: #include<iostream…
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 triang…
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes…
每个字符串看成一个物品,两个属性是0和1的个数,转换为01背包. code class Solution { public: int w[605][2]; int dp[105][105]; int v[605]={1}; int findMaxForm(vector<string>& strs, int m, int n) { int len=strs.size(); for(int i=0;i<len;i++){ int siz=strs[i].size(); for(int…
题意:给出n条边,用这n条边构成一个三角形,求三角形的最大面积. 先求面积,用海伦公式,s=sqrt(p*(p-a)*(p-b)*(p-c)),其中a,b,c分别为三角形的三条边,p为三角形的半周长,同时由这个根式可以推出,三角形的任意一条边小于其半周长(根号里面大于0,如果等于0面积为0没有意义了) 所以考虑背包两条边,再用周长减去这两条边求出第三条边,再遍历一遍找出最大的三角形. dp[i][j]表示三角形的第一条边为i,第二条边为j所构成的三角形是否存在, 如果存在,dp[i][j]=1,…
描述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 pastur…
题意:给出不多于40个小棍的长度,求出用所有小棍组成的三角形的最大面积. 思路:三角形3边求面积,海伦公式:p=(a+b+c)/2;S=p*(p-a)*(p-b)*(p-c);因为最大周长为1600  则三角形的边长小于周长一半800: 则可以用背包思想dp[i][j]代表能组成的两条边i和j.dp为true代表小棍能组成这两条边,第三条边也能求出,最后遍历一次dp数组求最大面积. 代码: #include<iostream> #include<cstring> #include&…
T了两发,DP方程很简单粗暴 dp[i][j][k]:用前i物品使得容量分别为j和k的背包恰好装满 背包的调用只需一次即可,第一次T就是每次check都丧心病狂地背包一次 对于sum的枚举,其实i j枚举到sum/2就可以了 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #define scan(a) scanf(…
http://www.cnblogs.com/ziyi--caolu/p/3228090.html http://blog.csdn.net/lyy289065406/article/details/6648094/ 这道题更加理解了背包问题实质上是状态的转换. 范围中有负数,先平移到全是正数,因为最后所有砝码都要用上,所以可以先遍历第一个的所有情况,再以此推出第二个. 上面的思路用穷举是不行的,所以这时就要用DP,而背包正好很适合解决这一类问题,这就是解题的突破口 #include <iost…