POJ1837-Balance(01背包)

来源:互联网 发布:html调用java方法 编辑:程序博客网 时间:2024/05/18 03:13

POJ1837 原题链接:http://poj.org/problem?id=1837

Balance
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 15207 Accepted: 9554

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



题目大意:

给一个天平与C个挂钩,G个砝码,挂钩的负数表示在天平左边,正数表示在右边,求把砝码放完时有多少种放法可以使天平平衡。

样例输入分析:

2 4(有2个挂钩,4个砝码)

-2 3(臂长为2的挂钩在天平左边,臂长为3的挂钩在右边)

3 4 5 8(四个砝码的重量)

共有2种方法时天平平衡(砝码要全部用完!这里被坑了好久)


思路:

首先想到的是一个个找,后来想了想20*20肯定超时就放弃了。

看了别人的博客才知道用01背包。

1.天平平衡时说明 (左)臂长*砝码重量=臂长*砝码重量(右)可以引入一个平衡度j的概念来表示天平是否平衡

当j=0时说明天平达到平衡状态。

2.在01背包中dp[i][j](i表示放入第i个物品,j表示背包的空间,dp[i][j]表示放入第i个物品时背包的最大价值)

   在这道题中dp[i][j](i表示挂满第i个砝码,j表示平衡度的值,dp[i][j]表示挂上第i个砝码时天平达到平衡度j的方法数)

   当然在这道题中还要考虑挂钩的位置(臂长);

3.为叙述方便将砝码重量设为g[i],挂钩位置设为c[n].

   那么每次挂上一个钩码后,对平衡状态的影响因素就是每个钩码的力臂=重量 *臂长 = g[i]*c[n]

   既然是dp,那么就要找到转移方程,假设dp[i-1][j]的值已知为num;那么对于dp[i][j]来说有两种决策,就是选第i个砝码放      在哪个位置或者不放。所以dp[i][j]+=dp[i-1][j-g[i]*c[n]];

   dp[i][j]=dp[i-1][j-g[i]*c[n]]+dp[i][j]

  f[i,j]=Max{f[i-1,j-Wi]+Pi( j >= Wi ),  f[i-1,j]}

   (1).比较两个方程可以发现这道题的方程少了比较与dp[i-1][j]这是由于题目要求为挂满时的方法数所以一定要放.

   (2).可以发现第二个方程的Pi在第一个方程中表现为dp[i][j],第二个方程中Pi表示价值,这里dp[i][j]表示方法数

4.因为下标不能为负数,而平衡度的范围为20*15*25=7500,即-7500~7500,可以平移到0~15000;


代码如下:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int dp[21][15001]; //状态数组dp[i][j]                       //放入(挂上)前i个物品(钩码)后,达到j状态的方法数int main(){    int c[21];  //挂钩位置    int g[21];  //钩码重量    int C;  //挂钩数    int G;  //钩码数    cin>>C>>G;    for(int i=1;i<=C;i++)    {        cin>>c[i];    }    for(int i=1;i<=G;i++)    {        cin>>g[i];    }    memset(dp,0,sizeof(dp));    dp[0][7500]=1;    for(int i=1;i<=G;i++)    {        for(int j=1;j<=15000;j++)        {            for(int k=1;k<=C;k++)               if(j>=c[k]*g[i])                dp[i][j]+=dp[i-1][j-c[k]*g[i]];        }    }    cout<<dp[G][7500]<<endl;}




原创粉丝点击