关闭

POJ 1837 dp

标签: pojdp
89人阅读 评论(0) 收藏 举报
分类:

Balance
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 12209 Accepted: 7650
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

题意:
给一个有很多钩子的天平,和一些砝码,在将砝码全部用完的情况下。问有多少种放法可以平衡。
解法:
考虑dp,对每个砝码来说放在不同的位置造成的力矩不一样。总共有C*G种力矩(C为力臂个数,G为砝码个数)我们要做的就是就是从这C *G个力矩中选出相加为0的情况个数。(注意,每个砝码必须用到)我们假设dp [ i ] [ j ]为选前i个砝码力矩矢量和为j的情况。由于每个砝码必须取。那么当取到第i个砝码时,结果必须依赖前一步也就是dp[i-1]的情况(如果没有依赖关系就不是动态规划问题了。)由此,状态转移方程为:
dp[i][k + C[i]*G[j]] += dp[i-1][k].当时推到这里,觉得可以空间压缩。就直接吧dp数组变成了一个一维数组。dp[i]直接继承dp[i]的所有情况。但WA了后发现有问题。由于第i层的所有状态都是由第i-1层推来到的。那么根据转移方程dp[i][k + C[i]*G[j]] += dp[i-1][k]有可能在第i-1层有的状态到i层转移后就没有了。所以不能直接继承i-1的状态。那么就无法空间压缩。由于力矩有负数,而数组下标不可能是负数。因此需要合适的对负数处理。这里用了一个大数字,负数下标的都放在越过这个大数字的位置。那么dp[i][0]就是平衡状态。当没有放任何东西的时候,应该就是平衡的,所以dp[0][0] = 1开始初始化错了,WA了。

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

int G[30][30];
int C[30];
int W[30];
int dp[30][200000];

int main(){
//    freopen("data.in","r",stdin);
//    freopen("data.out","w",stdout);
    int x,y;
    scanf("%d%d",&y,&x);
    int i,j,k;
    for(i = 0;i<y;i++) scanf("%d",&C[i]);
    for(i = 1;i<=x;i++) scanf("%d",&W[i]);
    int sum1 = 0,sum2 = 0;
    for(i = 1;i<=x;i++)
        for(j = 0;j<y;j++){
            G[i][j] = W[i]*C[j];
            if(G[i][j]>=0) sum1+=G[i][j];
            else sum2 += G[i][j]*(-1);
        }
    int Max = max(sum1,sum2);

    int yy;
    memset(dp,0,sizeof(dp));
    dp[0][0] = 1;
    for(i = 1;i<=x;i++){
        for(j = 0;j<y;j++){
            if(i == 0){
                if(G[i][j]<0)
                    yy = Max - 1 + -1*G[i][j];
                else
                    yy = G[i][j];
                dp[i][yy] = 1;
            }else{
                for(k = 0;k<2*Max;k++){
                    if(dp[i-1][k]){
                        if(k<Max){
                            if(k+G[i][j]<0)
                                yy = Max - 1 + -1*(k+G[i][j]);
                            else
                                yy = k+G[i][j];
                        }else{
                            if(-1*(k - Max + 1) +G[i][j]<0)
                                yy = Max - 1 + -1*(-1*(k - Max + 1)+G[i][j]);
                            else
                                yy = -1*(k - Max + 1)+G[i][j];

                        }
                        dp[i][yy] += dp[i-1][k];
                    }
                }
            }
        }
    }

    printf("%d",dp[x][0]);
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32775次
    • 积分:2041
    • 等级:
    • 排名:第19684名
    • 原创:179篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论