POJ 1837 dp

原创 2015年11月18日 11:00:24

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;
}
版权声明:博客迁移到 yaokun.wiki

poj 1837 Balance -dp-01背包

Balance Gigel has a strange "balance" and he wants to poise it. Actually, the device is different fr...
  • ly59782
  • ly59782
  • 2016年10月22日 16:44
  • 140

POJ-1837 天平dp

BalanceTime Limit: 1000MS Memory Limit: 30000KDescription Gigel has a strange “balance” and he...

poj1837——Balance(dp)

DescriptionGigel has a strange “balance” and he wants to poise it. Actually, the device is different...

POJ 1837 Balance(二维dp)

题意: 一个天平,给n个不同长度(g[i])方向的钩子,m个不同重量(w[i])砝码,问有多少钟方法在使用了所有砝码后使得天平平衡. 思路: 一开始想的 以为钩子也要全部用上,这样就比较简单了,不...

POJ 1837 Balance (DP)

Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is differe...

文章标题 poj 1837: Balance(dp)

Funny Car RacingDescriptionThere is a funny car racing in a city with n junctions and m directed roa...

POJ 1837 Balance DP 二维背包

传送门:POJ 1837 Balance分析: g个挂钩挂上的极限值:15*25*20==7500。 那么在有负数的情况下是-7500~~7500 此时的平衡点为0。 我们当然可以将平衡点往右...

POJ1837 Balance(DP)

题意: 一个杠杆问题,给出一些悬挂位置,正数代表右边,负数代表左边,再给出一些不同重量的砝码,要求输出有几种平衡的可能 要点: dp问题,主要是状态转移方程比较难写:dp[砝码i][力矩k+砝码...

[DP]poj1837

题意: 有一个天平,给出天平中可以放砝码的位置,负数为左边,正数为右边。然后给出g个砝码,要求,砝码全部使用,问最终达到平衡的状态的情况数。分析: 仍然感觉自己没能掌握使用dp的正确姿势。。。 ...

poj1837 Balance 看似dfs但是是枚举(dp)

Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is differe...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1837 dp
举报原因:
原因补充:

(最多只允许输入30个字)