POJ1873 - Balance(01背包)
题目大意
现有一个天平,它有C个挂钩和G个砝码,问有多少种方法可以使得天平平衡(砝码必须用完)
题解
其实就是让背包容量为0的方法有多少种方法,但是这样的话背包容量会出现负数,所以可以平移一下,背包容量最大值为20*25*15=7500,即平移量为7500,最后答案就是dp[G][7500]
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 7500
int dp[25][MAXN*2+5];
int a[25],w[25];
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=m; i++)
cin>>w[i];
dp[0][MAXN]=1;
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++)
{
int value=w[i]*a[j];
for(int k=0; k<=MAXN*2; k++)
if(dp[i-1][k])
dp[i][k+value]+=dp[i-1][k];
}
cout<<dp[m][MAXN]<<endl;
}
return 0;
}
POJ1873 - Balance(01背包)的更多相关文章
- Balance(01背包)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9163 Accepted: 5617 Description Gigel ...
- POJ 1837 Balance 01背包
题目: http://poj.org/problem?id=1837 感觉dp的题目都很难做,这道题如果不看题解不知道憋到毕业能不能做出来,转化成了01背包问题,很神奇.. #include < ...
- POJ 1837 Balance(01背包变形, 枚举DP)
Q: dp 数组应该怎么设置? A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数 题意: 有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣. 2 4 - ...
- HDU 5616 Jam's balance(01背包)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- Jam's balance HDU - 5616 (01背包基础题)
Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side ...
- poj 1837 01背包
Balance Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
随机推荐
- 几种更新(Update语句)查询的方法【转】
正 文: 数据库更新就一种方法Update,其标准格式:Update 表名 set 字段=值 where 条件不过根据数据的来源不同,还是有所区别的: 1.从外部输入这种比较简单例:update t ...
- C#微信登录-电脑版扫描二维码登录
像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...
- MVC-内容详情页显示内容
@model InfoDataProvider.DataModel.FAQ_ContentUser 内容Content字段:如果里面有html标签. @Html.DisplayFor(p => ...
- MVC-简单验证码制作
1.制作验证码: using System; using System.Collections.Generic; using System.Drawing; using System.Drawing. ...
- MongoDB索引介绍
MongoDB中的索引其实类似于关系型数据库,都是为了提高查询和排序的效率的,并且实现原理也基本一致.由于集合中的键(字段)可以是普通数据类型,也可以是子文档.MongoDB可以在各种类型的键上创建索 ...
- stdout 编码 vim 删除左边,右边
sys.stdout = codecs.getwriter('utf8')(sys.stdout) vimdic['kkkk'] = qqqqqdic['bbbb'] = aaaaaadic['kkk ...
- jquery ashx
http://www.cnblogs.com/wzcheng/archive/2010/05/20/1739810.html http://www.cnblogs.com/yyl8781697/arc ...
- urllib2.urlopen超时问题
urllib2.urlopen超时问题 没有设置timeout参数,结果在网络环境不好的情况下,时常出现read()方法没有任何反应的问题,程序卡死在read()方法里,搞了大半天,才找到问题,给ur ...
- ASP.NET MVC中HttpContext, HttpContextBase, HttpContextWrapper联系
ttpContext HttpContext是最原始的ASP.NET Context. MVC的目的之一是能够单元测试.HttpContext没有base class,并且不是virtual,所以不能 ...
- *[topcoder]HexagonalBoard
http://community.topcoder.com/stat?c=problem_statement&pm=12784 真心觉得tc的div1 250不少好题,对我来说比较适合.这道题 ...