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.
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.
• 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
题意:一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。其中可以把天枰看做一个以x轴0点作为平衡点的横轴
思路:钩码和挂钩式各不相同的,所以每一个钩码挂在不同的的挂钩上也唯一对应一个值,我们称之为平衡值,开始时没有挂钩是平衡的然后开始挂钩码,平衡值开始变化每次找到上一个钩码挂上后能够达到的平衡值进行平衡值变化操作。dp[i][j] 表示在挂满前i个物体的时,平衡度为j的挂法的数量。j为正表示右面重。最极端的情况是所有物体都挂在最远端,因此平衡度最大值为15*20*25=7500。原则上就应该有dp[ 0..20 ][-7500 .. 7500 ]。因此做一个处理,使得数组开为 dp[0.. 20][0..15000]。然后利用滚动数组进行优化;
AC代码:
未用滚动数组:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 int dp[22][7500*2+3]={0}; 7 int sgin[23]={0}; 8 int gou[23]={0}; 9 10 11 int main() 12 { 13 // freopen("1.txt","r",stdin); 14 int c,g; 15 cin>>c>>g; 16 int i; 17 for(i=1;i<=c;i++) 18 cin>>gou[i]; 19 int a; 20 int s=1; 21 cin>>a; 22 for(i=1;i<=c;i++){ 23 dp[s][gou[i]*a+7500]=1; 24 } 25 s++; 26 while(s<=g){ 27 cin>>a; 28 for(i=1;i<=c;i++){ 29 sgin[i]=gou[i]*a; 30 } 31 for(i=0;i<=7500*2+1;i++){ 32 if(dp[s-1][i]){ 33 for(int j=1;j<=c;j++){ 34 dp[s][sgin[j]+i]+=dp[s-1][i]; 35 } 36 } 37 } 38 s++; 39 } 40 cout<<dp[g][7500]<<endl; 41 return 0; 42 }
使用滚动数组优化:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 int dp[2][7500*2+3]={0}; 7 int sgin[23]={0}; 8 int gou[23]={0}; 9 10 11 int main() 12 { 13 // freopen("1.txt","r",stdin); 14 int c,g; 15 cin>>c>>g; 16 int i; 17 for(i=1;i<=c;i++) 18 cin>>gou[i]; 19 int a; 20 cin>>a; 21 for(i=1;i<=c;i++){ 22 dp[g%2][gou[i]*a+7500]=1; 23 } 24 g--; 25 while(g){ 26 cin>>a; 27 for(i=1;i<=c;i++){ 28 sgin[i]=gou[i]*a; 29 } 30 memset(dp[g%2],0,sizeof(dp[g%2])); 31 for(i=0;i<=7500*2+1;i++){ 32 if(dp[1-g%2][i]){ 33 for(int j=1;j<=c;j++){ 34 dp[g%2][sgin[j]+i]+=dp[1-g%2][i]; 35 } 36 } 37 } 38 g--; 39 } 40 cout<<dp[1][7500]<<endl; 41 return 0; 42 }