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
2 4
-2 3
3 4 5 8
Sample Output
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] 就足够了
代码:
#include <iostream>
using namespace std;
const int shift = 7500;
int C, G;
int c[21], g[21];
int dp[21][25000];
int solve_dp() {
memset(dp, 0, sizeof(dp));
dp[0][shift] = 1;
for(int i = 1; i <= G; i++) {
for(int v = 0; v<= 2*shift; v++) {
if(dp[i-1][v])
for(int j = 0; j < C; j++) {
dp[i][v+c[j]*g[i]] += dp[i-1][v];
printf("dp[%d][%d] = %d\n",i, v+c[j]*g[i]-7500,dp[i][v+c[j]*g[i]]);
}
}
}
return dp[G][shift];
}
int main() {
freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin);
cin >> C >> G;
for(int i = 0; i < C; i ++)
scanf("%d", &c[i]);
for(int i = 1; i <= G; i ++)
scanf("%d", &g[i]);
// mainfcun
cout << solve_dp() << endl;
return 0;
}
update 2014年3月14日14:59:57
再次做, 仍然毫无思路
本体的所有难点都在动态规划的设计上, dp[][] 并不是方案数, 而是放入第 i 个秤砣后的平衡系数