Q: dp 数组应该怎么设置?

A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数

题意:

有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣.

2 4

-2 3

3 4 5 8

C = -2, G = 3, 那么

2*(3+4+5)=3*(8); 2*(4+8)=3*(3+5)

共有两种可行的方案, 那么结果就是2

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

  1. 2 4
  2. -2 3
  3. 3 4 5 8

Sample Output

  1. 2

思路:

  1. 令 dp[i][j] 表示将第 I 件物品放入天平后, 平衡度为 j 的方案数, 平衡度可能为负, 可加入偏移使其总是正数

  2. dp[i][v+w[i]*h[j]] += dp[i-1][v]

总结

  1. 按照黑书的划分, 这道题既是把问题看成多阶段的决策过程, 也是利用记忆化搜索解决重叠子问题

  2. 本打算使用滚动数组来做, 后来发现滚动数组难以初始化, 就作罢了

  3. 代码第二层循环, v 的取值范围. 因为 dp[0][shift] = 1 保证以后的 v+c[i]*g[i] 不会出现小于 0 的情况发生. 因为 shift = 7500, 最大能偏 7500. 我最初理解错误, 以为  需要通过设置 v 的取值范围才能保证 v 不为负. 其实只要设置 dp[0][shift] 就足够了

代码:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int shift = 7500;
  5. int C, G;
  6. int c[21], g[21];
  7. int dp[21][25000];
  8.  
  9. int solve_dp() {
  10. memset(dp, 0, sizeof(dp));
  11. dp[0][shift] = 1;
  12. for(int i = 1; i <= G; i++) {
  13. for(int v = 0; v<= 2*shift; v++) {
  14. if(dp[i-1][v])
  15. for(int j = 0; j < C; j++) {
  16. dp[i][v+c[j]*g[i]] += dp[i-1][v];
  17. printf("dp[%d][%d] = %d\n",i, v+c[j]*g[i]-7500,dp[i][v+c[j]*g[i]]);
  18. }
  19. }
  20. }
  21. return dp[G][shift];
  22. }
  23.  
  24. int main() {
  25. freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin);
  26. cin >> C >> G;
  27. for(int i = 0; i < C; i ++)
  28. scanf("%d", &c[i]);
  29. for(int i = 1; i <= G; i ++)
  30. scanf("%d", &g[i]);
  31. // mainfcun
  32. cout << solve_dp() << endl;
  33. return 0;
  34. }

  

update 2014年3月14日14:59:57

再次做, 仍然毫无思路

本体的所有难点都在动态规划的设计上, dp[][] 并不是方案数, 而是放入第 i 个秤砣后的平衡系数

POJ 1837 Balance(01背包变形, 枚举DP)的更多相关文章

  1. POJ 1837 Balance 01背包

    题目: http://poj.org/problem?id=1837 感觉dp的题目都很难做,这道题如果不看题解不知道憋到毕业能不能做出来,转化成了01背包问题,很神奇.. #include < ...

  2. POJ 2923 Relocation(01背包变形, 状态压缩DP)

    Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...

  3. POJ 1837 -- Balance(DP)

     POJ 1837 -- Balance 转载:優YoU   http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...

  4. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  5. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  6. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  8. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  9. uestc oj 1218 Pick The Sticks (01背包变形)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...

随机推荐

  1. 《SPARK/TACHYON:基于内存的分布式存储系统》-史鸣飞(英特尔亚太研发有限公司大数据软件部工程师)

    史鸣飞:大家好,我是叫史鸣飞,来自英特尔公司,接下来我向大家介绍一下Tachyon.我事先想了解一下大家有没有听说过Tachyon,或者是对Tachyon有没有一些了解?对Spark呢? 首先做一个介 ...

  2. codeBlocks编译undefined reference to错误

    是没有把c文件编译进去的原因. 右键项目,选择属性,弹出窗体 然后选择build targets 在最下面有个build target files:中把c文件勾选.点击ok重新编译即可. Code:: ...

  3. FreeRTOS 低功耗之待机模式

    STM32F103 如何进入待机模式在 FreeRTOS 系统中,让 STM32 进入待机模式比较容易,调用固件库函数PWR_EnterSTANDBYMode 即可. STM32F103 如何退出待机 ...

  4. PHP 友好的dump

    /** * 浏览器友好的变量输出 * @param mixed $var 变量 * @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串 * @par ...

  5. 4款基于html5 canvas充满想象力的重力特效

    今天给大家分享4个物理和重力实验,用来展示 html canvas 的强大.几年前,所有这些实验都必须使用 Java 或 Flash 才能做.在下面这些惊人的例子中,就个人而言,我比较喜欢仿真布料的那 ...

  6. Windows 2008 R2组策略之二——GPO初探

    与前一篇博文发表已经相去月余了,实在是有些不好意思了.感谢51CTO的大编们将小文加了推荐,这使我更加觉得自己产出太少,难于回馈大家的厚爱. 今天将这个系列的第二篇奉献给大家,以伺视听. 在上篇小文中 ...

  7. excel导出功能优化

    先说说优化前,怎么做EXCEL导出功能的: 1. 先定义一个VO类,类中的字段按照EXCEL的顺序定义,并且该类只能用于EXCEL导出使用,不能随便修改. 2. 将查询到的结果集循环写入到这个VO类中 ...

  8. pip国内源

    pip install -i https://pypi.douban.com/simple pyqrcode

  9. 对设计领域中Tile和Card的理解

    前端工程师离不开设计, 谈到设计就要想到大名鼎鼎的material design主题, 而material是以card为经典单元的, card即卡片, 是层次化模型的最小模块, 用于提供扁平化的信息, ...

  10. 免费CA数字证书的申请、安装、导入、导出

    http://wenku.baidu.com/link?url=oDUw50eCE5zX8tmg4N3-ddYGLt1U5aJYGEN7rk_z7t6LuMHL3M4oBstYBI_dQ1UnCtcK ...