Balance

Problem 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
 
Source
PKU

题意:给出C个可以悬挂钩码的位置;再给出G个钩码的重量;求:将所有的钩码都悬挂上后,有多少情况可以使天平平衡;

思路:一开始想的是利用深搜的方法将所有的情况搜索一遍,但是20^20时间太长;所以超时了;

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 
 4 using namespace std;
 5 
 6 
 7 int main()
 8 {
 9     freopen("1.txt","r",stdin);
10     int c,g;
11     cin>>c>>g;//输入C和G的值;
12     int numc[23],numg[23];
13     int dp[21][15010]={0};
14     int i,j,s;
15     int sum=0;;
16     for(i=0;i<c;i++)cin>>numc[i];//录入可以悬挂钩码的点的位置;
17     for(i=0;i<g;i++)cin>>numg[i];//录入各个钩码的重量;
18     for(i=0;i<c;i++)dp[0][7500+numc[i]*numg[0]]=1;//初始化dp数组,即第一个钩码放在各个点的时候的值所对应的dp数组中值赋值为1;
19     for(i=0;i<g-1;i++){
20         for(s=0;s<15001;s++)
21             if(dp[i][s]!=0)//达到改点的情况不为零;
22                 for(j=0;j<c;j++){dp[i+1][s+numc[j]*numg[i+1]]+=dp[i][s];}//递推方程;
23     }
24     cout<<dp[g-1][7500]<<endl;
25     return 0;
26 }
View Code

 

 

 

 

posted @ 2013-08-02 08:36  秋心无波  阅读(429)  评论(0编辑  收藏  举报