POJ1837--二维背包
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 13525 | Accepted: 8474 |
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
题目大意:给定一个天平,然后给定位置和一定数量的砝码,问有多少种方法能够使得天平达到平衡(注意:给定的砝码每一种只有一个,但是需要全部用完)
样例: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--二维背包的更多相关文章
- 二维背包(钟神想要的)(不是DP)
[问题描述] 背包是个好东西,希望我也有.给你一个二维的背包,它的体积是? × ?.现在你有一些大小为1× 2和1×3的物品,每个物品有自己的价值.你希望往背包里面装一些物品,使得它们的价值和最大,问 ...
- hdu 4501 小明系列故事——买年货_二维背包
题目:你可以有v1元,v2代金券,v3个物品免单,现在有n个商品,商品能用纸币或者代金券购买,当然你可以买v3个商品免费.问怎么最大能买多少价值 题意: 思路二维背包,dp[v1][v2][v3]=M ...
- HDU 2159 FATE (二维背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 解题报告:这题实际上是一个二维的背包问题,也可以由01背包扩展而来,01背包用一维数组,可想而知 ...
- rqnoj-329-刘翔!加油!-二维背包
注意排除干扰项. 因为价值不会相等,所以价值的多少与本题没有任何关系,. 所以价值为干扰项,所以不用考虑. 二维背包,简单求解. #include<stdio.h> #include< ...
- NOI 4978 宠物小精灵之收服(二维背包)
http://noi.openjudge.cn/ch0206/4978/ 描述 宠物小精灵是一部讲述小智和他的搭档皮卡丘一起冒险的故事. 一天,小智和皮卡丘来到了小精灵狩猎场,里面有很多珍贵的野生宠物 ...
- dp之二维背包poj2576
题意:有一群sb要拔河,把这群sb分为两拨,两拨sb数只差不能大于1,输出这两拨人的体重,小的在前面...... 思路:把总人数除2,总重量除2,之后你会发现就是个简单的二维背包,有两个限制..... ...
- hdu 3496 Watch The Movie (二维背包)
Watch The Movie Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- 二维背包---P1509 找啊找啊找GF
P1509 找啊找啊找GF 题解 很明显这是一道二维背包题目 如果一个dp数组做不了,那么我们就再来一个dp数组 题目要求,花费不超过 m ,消耗人品不超过 r ,泡到尽量多的妹子,时间尽量少 f ...
- 二维背包---P1855 榨取kkksc03
P1855 榨取kkksc03 题解 二维背包板子题 f[ i ][ j ] 前 n 个物品,花费金钱不超过 i ,花费时间不超过 j 的最大价值 如果每个物品只能选一次,那么就相当于在01背包上多加 ...
- 01二维背包——poj2576
/* 要求把a数组分成两个集合,两个集合人数最多差1,并且元素之和的差尽可能小 那只要把所有可行的列出来即可 01二维背包,即体积是个二维数据,那么我们的背包状态也应该设为二维 dp[j][k]设为 ...
随机推荐
- PHP二分查找算法
思路:递归算法.在一个已经排好序的数组中查找某一个数值,每一次都先跟数组的中间元素进行比较,若相等则返回中间元素的位置,若小于中间元素,则在数组中小于中间元素的部分查找,若大于中间元素,则在数组中大于 ...
- LeetCode 88. Merge Sorted Array(合并有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- LeetCode 48. Rotate Image(旋转图像)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 微信小程序支付
@Controllerpublic class UserPayToMerchantController { public static final String appid="******* ...
- bootstrap折叠调用collapse()后data-parent不生效问题
今天做的项目,用到了bootstrap的折叠功能,这个功能需要只展开一个折叠框,点击一个就会自动隐藏另一个,初始按照API做了一下,发现一切运行正常,但是测试的同事提了一个bug,说切换到其他模块后再 ...
- 小程序组件之picker和range-key的用法
因为在微信小程序的官网上并没有range-key的例子以及实际用法,所以好多人不知道具体如何使用.然后我在这里对其进行一个简单的实现,并记录一些注意事项. 以下是官网给的说明: 具体的用法 ...
- K - Kia's Calculation (贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- JAVA中文乱码之解决方案
1.解决HTML页面的中文问题:为了使HTML页面很好的支持中文,在每个HTML页面的<head>标签内部增加(创建HTML页面自带) <head> <meta char ...
- PHP实现前台页面与MySQL的数据绑定、同步更新
今天我来给大家介绍一个PHP-MySQL的小项目. 使用 PHP和前台Ajax 实现在前台对MySQL数据库中数据的增.删等操作语句功能. 如果有问题,欢迎拍砖~ 首先,我们先做好前台HTML.CSS ...
- Spring Boot单元测试(Mock)
Spring Boot单元测试(Mock) Java个人学习心得 2017-08-12 16:07 Mock 单元测试的重要性就不多说了,我这边的工程一般都是Spring Boot+Mybatis(详 ...