Balance

                                             Balance

                                                              Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)
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
 

题意:题目涉及到我们所学的物理知识,即力矩等于力臂乘以重量。题目意思是,一个细杆(不计重量)在中间被支撑,左右有几个挂钩,左边的为负值,右边的为正值,然后给出几个物体的重量,问有几种挂重物方法可以让细杆左右平衡。

输入 :第一行 :挂钩的数目,物体的数目;

         第二行:挂钩的位置;

         第三行:物体的重量。

思路:01背包。由于力矩 = 力 * 力臂,状态转移方程: dp[i][v + wei[i] * len[j]] += dp[i - 1][k]。另外因为有负数的出现,这点与普通的题不同,所以要用到偏移量temp。

源代码:

#include<iostream>
#include<cstring>
using namespace std;
const int mMax = 22;
const int nMax = 15000;
const int temp = 7500;
int hoo[mMax], wei[mMax];
int dp[mMax][nMax];
int main()
{
    int c,g,i,j,v,val;
    while(cin>>c>>g)
    {
        for(i=1;i<=c;i++)
            cin>>hoo[i];
        for(i=1;i<=g;i++)
            cin>>wei[i];
        memset(dp,0,sizeof(dp));
        dp[0][temp]=1;
        for(i=1;i<=g;i++)
            for(j=1;j<=c;j++)
            {
                val=wei[i]*hoo[j];
                for(v=0;v<nMax;v++)
                    if(dp[i-1][v] != 0)
                        dp[i][val+v] += dp[i-1][v];
            }
       cout<<dp[g][temp]<<endl;
    }
    return 0;
}

 

 

 

 

posted on 2013-10-04 18:44  天梦Interact  阅读(353)  评论(0编辑  收藏  举报