poj 1837 Balance (dp,01背包)

链接:poj 1837

题意:有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,

求将钩码挂到钩子上使天平平衡的方法的总数。其中可以把天枰看做一个以x轴0点作为平衡点的横轴

分析:力臂=重量 *臂长 = g[i]*c[j]

当平衡度k=0时,说明天枰达到平衡,k>0,说明天枰倾向右边(x轴右半轴),k<0则左倾

因此可以定义一个 状态数组dp[i][k],意为在挂满前i个钩码时,平衡度为k的挂法的数量。

由于距离c[i]的范围是-15~15,钩码重量的范围是1~25,钩码数量最大是20

因此最极端的平衡度是所有物体都挂在最远端,因此平衡度最大值为j=15*20*25=7500

原则上就应该有dp[ 1~20 ][-7500 ~ 7500 ]。

因此为了不让下标出现负数,做一个处理,使使得数组开为 dp[1~20][0~15000],则当j=7500时天枰为平衡状态

状态方程为 dp[i][k]+=dp[i-1][k-g[i]*c[j]];

#include<stdio.h>
#include<string.h>
int dp[21][15010];
int main()
{
    int m,n,i,j,k,c[21],g[21];
    scanf("%d%d",&m,&n);
    for(i=1;i<=m;i++)
        scanf("%d",&c[i]);
    for(i=1;i<=n;i++)
        scanf("%d",&g[i]);
    memset(dp,0,sizeof(dp));
    dp[0][7500]=1;
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
            for(k=g[i]*c[j];k<=15000;k++)
                dp[i][k]+=dp[i-1][k-g[i]*c[j]];
    printf("%d\n",dp[n][7500]);
    return 0;
}

poj 1837 Balance (dp,01背包)

时间: 2024-10-09 22:04:58

poj 1837 Balance (dp,01背包)的相关文章

poj 1947(树形DP+01背包)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10663   Accepted: 4891 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. Th

POJ 1837 Balance DP

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

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (Interna

POJ 1837 Balance

题意:给你C个挂钩,W个钩码,要你能使一个天平平衡 数据解释: 2 4 -2 3 3 4 5 8 以原点为支点,那么-2代表支点左边2处有一个钩码,同理3代表右边的点 所以案例数据有一个成立的例子是(3+5)*3=(4+8)*2或是(3+4+5)*2=8*3(力臂平衡) 有2种情况所以输出2: 思路:这个如果不是按照题目的分类说是DP我还想不到这个思路,我感觉我进步挺大了,能独立推出转移方程了. 首先我们看这道题首先是要求力平衡,那么一个限制是重量.与力相关的有钩码与挂钩的位置.显然,钩码可以放

POJ 3628 Bookshelf 2 (01背包)

Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available

USACO Money Systems Dp 01背包

一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V; ++j){ if(i - a[j] > 0){ dp[i] += dp[i - a[j]]; } } } 状态存在冗余, 输出的时候答案肯定不对 但只需要改一下两个for循环的顺序即可. Source Code: /* ID: wushuai2 PROG: money LANG: C++ */ //

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

Bone Collector(dp 01背包)

Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This man like to collect varies of bones , such as dog's , cow's , also he went to the grave - The bone collector had a big bag with a volu