Balance(01背包)

POJ - 1837 

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

题意:一个balance上面有C个可以放可放砝码的盘子(负数代表左边的位置,正数代表右边的位置),有G个砝码,问将所有砝码放在balance上,该balance有多少种平衡方式。

题解:01背包,dp[i][j],表示前i个砝码,平衡状态为j的方式。循环有三重,i:前i个砝码;j:状态为j;k:第i个砝码放在k位置(因为需要把砝码放完),状态转移方程为dp[i][j] += dp[i-1][j-b[i]*a[k]],因为状态处在负数,所以需要平移一下原点(O),const int O    =  1e5。初始化dp[0][O] = 1,意义为前0个砝码,状态为O的方式为1。

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e5;
const int mod  =  1e4 + 7;
const int maxn =  2e5+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

int main(){
    int C, G ;
    while(~scanf("%d%d", &C, &G)){
        int a[25], b[25];
        int Max = 0, Min = 0, sum = 0;
        for(int i=0; i<C; i++) {
            scanf("%d", &a[i]);
            (a[i] >=0 ? Max : Min) += a[i];
        }
        for(int i=1; i<=G; i++) {
            scanf("%d", &b[i]); sum += b[i];
        }
        Max *= sum; Min *= sum;//Min表示最小状态,Max表示最大状态
        LL dp[2][maxn]; MT(dp, 0); dp[0][O] = 1;
        //因为第i个砝码的状态只与i-1状态有关,所有可以优化一下内存(其实也没有必要,因为数据很小)
        for(int i=1; i<=G; i++) {
            for(int j=O+Min; j<=O+Max; j++){
                int sum = 0;
                for(int k=0; k<C; k++){
                    sum += dp[!(i&1)][j-b[i]*a[k]];
                }
                dp[i&1][j] = sum;
            }
        }
        LL ans = dp[G&1][O];
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mannix_Y/article/details/84777260