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 ...
随机推荐
- myeclipse10.0优化
一.Myeclipse10修改字体 MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉general-〉 Appearance->Colors ...
- apache+tomcat 负载均衡
说明:本篇不面向无开发基础的人员,所以不会有软件下载,jdk安装等步骤.比较久远的东西了...... 1.系统环境 win7-64.jdk 2.软件列表: apache_2.2.24-x64-no-s ...
- [转载]C#读写配置文件(XML文件)
.xml文件格式如下 [xhtml] view plaincopy <?xml version="1.0" encoding="utf-8"?> & ...
- Python图形图像处理库的介绍之Image模块
http://onlypython.group.iteye.com/group/wiki/1372-python-graphics-image-processing-library-introduce ...
- vc:如何从Internet上有效而稳定地下载文件
http://www.vckbase.com/index.php/wv/172 如何从Internet上有效而稳定地下载文件 ,这是很多网络应用程序要考虑的重要问题,本文提供的代码段针对这个问题进行了 ...
- 如何kill掉TaobaoProtect.exe
C:\Users\Administrator\AppData\Roaming\TaobaoProtect TaobaoProtect.exe https://technet.microsoft.com ...
- hdu4662MU Puzzle
http://acm.hdu.edu.cn/showproblem.php?pid=4662 I+3*U模6为2或4的都可以 一个U相当于3个I 而I只能1->2->4->8..如 ...
- 转自作者:phylips@bmy
差分约束系统 2008-11-28 20:53:25| 分类: 算法与acm|举报|字号 订阅 出处:http://duanple.blog.163.com/blog/static/7097 ...
- ECC校验优化之路
引子: 今天上嵌入式课程时,老师讲到Linux的文件系统,讲的重点是Linux中对于nand flash的ECC校验和纠错.上课很认真地听完,确实叹服代码作者的水平. 晚上特地下载了Linux最新的内 ...
- 【转】国内用户如何加快App Store的访问速度
原文网址:http://www.app111.com/doc/100024206_1.html 作为国内互联网用户是比较可怜的,除了国外四大顶尖互联网服务不能访问外,就是App Store用得也比较痛 ...