Balance
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 13525
Accepted: 8474

Description

Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. 

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 input has the following structure: 

• 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

The output contains the number M representing the number of possibilities to poise the balance.

Sample Input

2 4
-2 3
3 4 5 8

Sample Output

2

题目大意:给定一个天平,然后给定位置和一定数量的砝码,问有多少种方法能够使得天平达到平衡(注意:给定的砝码每一种只有一个,但是需要全部用完)

样例:2 4//第一个数代表有两个地方可以挂载砝码,4代表有4个砝码

-2 3//两个挂载位置,-2代表在天平左侧距离天平中心长度为2的地方,3代表在天平右侧距离天平中心长度为3的地方

3 4 5 8//分别代表4个砝码的质量

解题思路:

PS:参考了大神的思路(●'◡'●)

首先找出两个维度,第一维度为第几个砝码"i",第二个维度是天平的平衡度"j"。其中平衡度<0代表向左侧倾斜,平衡度>0代表向右侧倾斜,==0代表符合题目叙述要求。假设全部砝码挂在一端的外侧,那么最大的平衡度=15(天平臂长)*20(砝码数量)*25(最重的砝码数量)=7500。左侧为-7500,右侧为7500。避免负数带来的麻烦,整体偏移7500,得到0-15000,其中7500就是代表的原来的平衡位置。

再考虑状态转移方程,用dp[i][j]代表当挂第i个砝码时,平衡度能够达到j所产生的方法的数目。同时力矩=臂长*重量。

所以当dp[i][j]确定的时候,他能够向后影响dp[i+1][j+c[k]*w[i+1]]

注意:这里的k小标代表的是输入数据中的挂载位置,因为位置的不同,所以实际上dp[i][j]产生了k个影响。

那么站在任意一个dp[i+1][j+c[k]*w[i+1]]的情况来说,dp[i][j]为它产生了多少种新的方法呢?当然是dp[i][j]种,

即在原有的基础上增加了dp[i][j]种,就有dp[i+1][j+c[k]*w[i+1]]+=dp[i][j];

如果是dp[i-1][j]对dp[i][j+c[k]*w[i]]呢?同理,得到dp[i][j+w[i]*c[k]]+=dp[i-1][j];

所以,状态转移方程就是dp[i][j+w[i]*c[k]]+=dp[i-1][j]当然也可以刚开始推到的那一种,为了使用方便起见,用这一个,其实一样。

记得仔细想一想无向后性...

源代码:

<span style="font-size:24px;">#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdio.h>
using namespace std; int dp[21][15001];
int main() {
int i, j, k;//控制变量的下标
int n, g;
int c[21], w[21];
memset(dp,0,sizeof(dp));
dp[0][7500] = 1;//不放砝码时平衡度为7500至少有一种方法,初始化
scanf("%d%d",&n,&g);//位置个数,砝码的个数
for(i = 1; i <= n; i++) {
scanf("%d",&c[i]);
}
for(j = 1; j <= g; j++) {
scanf("%d",&w[j]);
}
for(i = 1; i <= g; i++) {
for(j = 0; j <= 7500*2; j++) {
//因为砝码所处位置的不同,所以需要再写for循环加和,才能够得到对应
//受到影响的dp[i][j+w[i]*c[k]],一个dp[i-1][j]产生n个影响
for(k = 1; k <= n; k++) {
dp[i][j+w[i]*c[k]]+=dp[i-1][j];
}
}
}
printf("%d\n",dp[g][7500]);//最后结果保存在dp[g][7500]中
return 0;
} </span>

POJ1837--二维背包的更多相关文章

  1. 二维背包(钟神想要的)(不是DP)

    [问题描述] 背包是个好东西,希望我也有.给你一个二维的背包,它的体积是? × ?.现在你有一些大小为1× 2和1×3的物品,每个物品有自己的价值.你希望往背包里面装一些物品,使得它们的价值和最大,问 ...

  2. hdu 4501 小明系列故事——买年货_二维背包

    题目:你可以有v1元,v2代金券,v3个物品免单,现在有n个商品,商品能用纸币或者代金券购买,当然你可以买v3个商品免费.问怎么最大能买多少价值 题意: 思路二维背包,dp[v1][v2][v3]=M ...

  3. HDU 2159 FATE (二维背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 解题报告:这题实际上是一个二维的背包问题,也可以由01背包扩展而来,01背包用一维数组,可想而知 ...

  4. rqnoj-329-刘翔!加油!-二维背包

    注意排除干扰项. 因为价值不会相等,所以价值的多少与本题没有任何关系,. 所以价值为干扰项,所以不用考虑. 二维背包,简单求解. #include<stdio.h> #include< ...

  5. NOI 4978 宠物小精灵之收服(二维背包)

    http://noi.openjudge.cn/ch0206/4978/ 描述 宠物小精灵是一部讲述小智和他的搭档皮卡丘一起冒险的故事. 一天,小智和皮卡丘来到了小精灵狩猎场,里面有很多珍贵的野生宠物 ...

  6. dp之二维背包poj2576

    题意:有一群sb要拔河,把这群sb分为两拨,两拨sb数只差不能大于1,输出这两拨人的体重,小的在前面...... 思路:把总人数除2,总重量除2,之后你会发现就是个简单的二维背包,有两个限制..... ...

  7. hdu 3496 Watch The Movie (二维背包)

    Watch The Movie Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  8. 二维背包---P1509 找啊找啊找GF

    P1509 找啊找啊找GF 题解 很明显这是一道二维背包题目 如果一个dp数组做不了,那么我们就再来一个dp数组 题目要求,花费不超过 m ,消耗人品不超过  r  ,泡到尽量多的妹子,时间尽量少 f ...

  9. 二维背包---P1855 榨取kkksc03

    P1855 榨取kkksc03 题解 二维背包板子题 f[ i ][ j ] 前 n 个物品,花费金钱不超过 i ,花费时间不超过 j 的最大价值 如果每个物品只能选一次,那么就相当于在01背包上多加 ...

  10. 01二维背包——poj2576

    /* 要求把a数组分成两个集合,两个集合人数最多差1,并且元素之和的差尽可能小 那只要把所有可行的列出来即可 01二维背包,即体积是个二维数据,那么我们的背包状态也应该设为二维 dp[j][k]设为 ...

随机推荐

  1. eval函数的坑

    开发工作中遇到这样一种情形,需要执行用户输入的php代码串,于是决定使用eval函数.coding大概示例如下: function getStr($str) { return strlen($str) ...

  2. 忘记root密码,进入单用户模式修改密码

    进入单用户模式 rhel61.在系统数秒时,按下键,进入到系统引导菜单 中2.选择系统后 按“e”键 选择kernel后 按“e”键 后空格 1+回车 b:启动系统 进入到单用户模式 rhel71.在 ...

  3. Linux上安装二进制文件MySQL详解

    前言:昨天晚上搞了很久,终于搞清楚mysql的安装配置了,我真是太low了.当我在云服务器上登进Mysql时,真是高兴哈哈,咱一步一步来,彻底搞懂Mysql的安装配置. 我的安装环境: 阿里云服务器 ...

  4. 快速配置vs2012+opencv

    关于OpenCV+Windows+VS配置的文章网上有很多,多是类似 OpenCV中文网 上的安装方法. 不管什么方法,配置的步骤毫无疑问是: 1. 配置环境变量, 2. 配置VS. 在这个过程中,令 ...

  5. struts2(五)之struts2拦截器与自定义拦截器

    前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...

  6. HDU 6113 度度熊的01世界

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. AngularJS学习篇(十七)

    AngularJS 输入验证 <!DOCTYPE html> <html> <script src="http://apps.bdimg.com/libs/an ...

  8. 【IDEA】向IntelliJ IDEA创建的项目导入Jar包的两种方式

    转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 今天用IDEA,需要导入一个Jar包,因为以前都是用eclipse的,所以对这个id ...

  9. elasticsearch 基础语句

    1.  doucument id 的两种生成方式 自动生成document id自动生成的id,长度为20个字符,URL安全,base64编码,GUID,分布式系统并行生成时不可能会发生冲突 POST ...

  10. [转载] Netty源码分析

    转载自http://blog.csdn.net/kobejayandy/article/details/11836813 Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高 ...