Balance_01背包
Description
It orders two arms of negligible weight and each arm's length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.
Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device.
It is guaranteed that will exist at least one solution for each test case at the evaluation.
Input
• the first line contains the number C (2 <= C <= 20) and the number G (2 <= G <= 20);
• the next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range -15..15 representing the repartition of the hooks; each number represents the position relative to the center of the balance on the X axis (when no weights are attached the device is balanced and lined up to the X axis; the absolute value of the distances represents the distance between the hook and the balance center and the sign of the numbers determines the arm of the balance to which the hook is attached: '-' for the left arm and '+' for the right arm);
• on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights' values.
Output
Sample Input
2 4
-2 3
3 4 5 8
Sample Output
2
【题意】给出一个左右两边的臂长都为15的天平,有n个挂钩,m个砝码,求多少的方法使他平衡
【思路】dp[i][j]表示i表示当前发码数,j表示当前平衡状态,不能为负,所以重新规定了平衡点15*20*25=7500;
j<7500左边重,反之右边重
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int M=**;
const int N=;
int c[N],g[N],dp[N][];
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<=n; i++)
{
scanf("%d",&c[i]);
}
for(int i=; i<=m; i++)
{
scanf("%d",&g[i]);
}
memset(dp,,sizeof(dp));
dp[][M]=;//0个砝码时为平衡状态为1种,小于7500为左边重,反之右边重
for(int i=; i<=m; i++)
{
for(int j=; j<=M*; j++)
if(dp[i-][j])
{
for(int k=; k<=n; k++)
{
dp[i][j+c[k]*g[i]]+=dp[i-][j];
}
}
}
printf("%d\n",dp[m][M]);
}
return ;
}
Balance_01背包的更多相关文章
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- HDU 3535 AreYouBusy (混合背包)
题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...
- HDU2159 二维完全背包
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- CF2.D 并查集+背包
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- 洛谷P1782 旅行商的背包[多重背包]
题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...
- POJ1717 Dominoes[背包DP]
Dominoes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6731 Accepted: 2234 Descript ...
- HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
随机推荐
- Maximal Rectangle [LeetCode]
Problem Description: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle co ...
- SAP smartforms之Zebra print control language
因为在做个小标签的时候需要将部分字符旋转180度,在scn上找了很久也发布了自己的提问,不过最终的结果却不尽人意.Rotated text in smartforms need use the PCL ...
- 支持新版chrome,用webstorm编译形成css和sourcemap,调试sass和less源文件(转)
旧版的chrome有个support for sass,但是新版chrome没有这个功能了.看到网上提供的方法比较多,也很乱,旧版新版的都有.而且不能指定自己所需要的路径. 所以就做了下改版. sas ...
- ios基础篇(五)——UITextField的详细使用
UItextFieldField通常用于外部数据输入,以实现人机交互. 以下是UItextFieldField的属性及常见用法: 1.textField :设置文本框的默认文本. 2.Placehol ...
- 开发excel 自定义func
http://www.cnblogs.com/brooks-dotnet/archive/2011/01/16/1936871.html http://club.excelhome.net/threa ...
- [转]Java 泛型: 什么是PECS(Producer Extends, Consumer Super)
以下内容转自: 原文链接: stackoverflow 翻译: ImportNew.com- 刘志军 译文链接: http://www.importnew.com/8966.html -------- ...
- 如何实现301的跳转?当输入域名http://xxx.com的时候自动重定向到www上去
答案:在服务器上操作,注意勾选和不勾选的区别,使用Fiddle进行观察,301和302之间的区别
- js获取浏览器的版本代码
<script>function GetXmlHttpObject(){var xmlHttp=null;var httptype='';try { // Firefox, Opera 8 ...
- 【工具推荐】ELMAH——可插拔错误日志工具
今天看到一篇文章(构建ASP.NET网站十大必备工具(2)),里面介绍了一个ELMAH的错误日志工具,于是研究了一下. ELMAH 是 Error Logging Modules and Handle ...
- 神奇的Noip模拟试题 T3 科技节 位运算
3 科技节 (scifest.pas/.c/.cpp) [问题描述] 一年一度的科技节即将到来.同学们报名各项活动的名单交到了方克顺校长那,结果校长一看皱了眉头:这帮学生热情竟然如此高涨,每个人都报那 ...