POJ1837 DP balance
Balance
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 7086 | Accepted: 4264 | 
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.
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
题意:一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。
其中可以把天枰看做一个以x轴0点作为平衡点的横轴
思路:一开始想DP,只想开一个C*G大小的数组,可是一直没有找出递推式,更无视了-15~15这个给的范围的用处,后来看了结题报告才明白。
开一个G*(平衡度)的数组,DP找的就是状态,所以平衡度就是该状态~~表示DP真的不好~~继续DP吧!
dp[i][j] 表示在挂满前i个物体的时,平衡度为j的挂法的数量。j为正表示右面重。最极端的情况是所有物体都挂在最远端,因此平衡度最大值为15*20*25=7500。原则上就应该有dp[ 0..20 ][-7500 .. 7500 ]。因此做一个处理,使得数组开为 dp[0.. 20][0..15000]。
现在说这个方程。dp[i][j]=sigma( dp[i-1][ j-c[k]*w[i] ] ), k=1~C, j=0~15000, i=1~20。初始状态 dp[0][7500]=1 表示不用物体时,平衡度为0有一种挂法,当然那就是什么都不挂
复杂度O(C*G*15000)
 View Code
View Code 
1 //memory 1396KB time 16ms 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 #include <string.h> 6 #include <stdlib.h> 7 using namespace std; 8 int dp[21][15001]; 9 int main() 10 { 11 int C,G,c[21],g[21]; 12 while(scanf("%d%d",&C,&G)!=EOF) 13 { 14 for(int i=1;i<=C;i++) 15 scanf("%d",&c[i]); 16 for(int i=1;i<=G;i++) 17 scanf("%d",&g[i]); 18 19 memset(dp,0,sizeof(dp)); 20 dp[0][7500]=1; 21 22 for(int i=1;i<=G;i++) 23 for(int j=0;j<=15000;j++) 24 if(dp[i-1][j]) 25 for(int k=1;k<=C;k++) 26 dp[i][j+g[i]*c[k]]+=dp[i-1][j]; 27 28 printf("%d\n",dp[G][7500]); 29 } 30 return 0; 31 }
在网上看到别人的有降低空间不过浪费时间的一个代码,可供参考:
 View Code
View Code 
1 //memory 285kb time 125ms 2 #include<cstdio> 3 #include<string> 4 #include<iostream> 5 #include<cstdlib> 6 int dp[15002],tp[15002],C,G; 7 int main() 8 { 9 int i,j,k,pos[26],wi[26]; 10 scanf("%d%d",&C,&G); 11 for(i=0;i<C;++i) 12 scanf("%d",&pos[i]); 13 for(i=0;i<G;++i) 14 scanf("%d",&wi[i]); 15 16 dp[7500]=1; 17 for(i=0;i<G;++i) 18 { 19 memcpy(tp,dp,sizeof(dp)); 20 memset(dp,0,sizeof(dp)); 21 for(j=-7500;j<=7500;j++) 22 for(k=0;k<C;++k) 23 dp[j+7500]+=tp[j+7500-pos[k]*wi[i]]; 24 } 25 printf("%d\n",dp[7500]); 26 return 0; 27 }
 
                    
                     
                    
                 
                    
                

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号