poj 1837 01背包

Balance

Time Limit: 1000 MS Memory Limit: 30000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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
/*
01背包

题意:C个钩码(2—20) G个物品(2—20) 钩码位置(-25—25) 物品重量(0—20)  物品都用上且天平平衡有多少种方案

dp[i][j]:挂前i个物品达到状态j 状态j的取值范围时-25*25*20——25*25*20  所以j取(-7500--7500) 防止出现负值 所以令j==15000  即j==7500时为平衡位置
想~~每次挂砝码都会影响天平的平衡 即状态j  影响因素是力臂=c[i]*w[k] (n,m影响它的取值)
     挂前i个物品时状态是dp[i-1][j] 则挂第i个物品后状态变为dp[i][j+c[i]*w[k]]
     假设dp[i-1][j]的值是num  那么  dp[i][j+c[i]*w[k]]也是num
     即dp[i][j+c[i]*w[k]]+=dp[i-1][j]   前面状态影响后面的  

 */
 #include <iostream>
 #include <string.h>
 #include <stdio.h>

 int dp[35][15001]; ///前i个物品达到j的状态有的dp[][]种

 int main()
 {
     int n,m;  ///钩子个数 砝码个数
     int c[35]; ///钩子的位置
     int w[35]; ///砝码重量

     scanf("%d%d",&n,&m);

     for(int i=1;i<=n;i++)
     scanf("%d",&c[i]);
     for(int j=1;j<=m;j++)
     scanf("%d",&w[j]);

     memset(dp,0,sizeof(dp));
     dp[0][7500]=1;   ///因为防止出现负数情况 所以dp[][1500]了  同时dp[][7500]是平衡状态

     for(int i=1;i<=m;i++)
     {
         for(int j=0;j<=15000;j++)
         {
             for(int k=1;k<=n;k++)
             {
                 dp[i][j+c[k]*w[i]]+=dp[i-1][j]; ///核心  在前面介绍
             }
         }
     }
     printf("%d\n",dp[m][7500]);
 }

poj 1837 01背包

时间: 2024-10-09 18:08:55

poj 1837 01背包的相关文章

poj 2184 0---1背包的变形

这题是0--1背包的变形,对理解0--1背包有很大的帮组 题意:要选一些牛去参见展览,每个牛有幽默.智慧两个选择标准,要求选的这些牛使得幽默和智慧的总和最大且幽默和智慧的每个总和都必须是大于等于0: 刚看的这个题目是时候,知道是一个0--1背包的的题目,但就是不知道怎么来写出状态转移方程,因为题中的两个变量都是有负值的. 看了大牛的解题报告才知道. 我们可以把幽默个变量看成是体积 , 智慧看成是价值. 我们可以把每个牛幽默的值 , 放在一个坐标上,让后整体往右移,使得最小值为 0 , 那么这时候

POJ 3624 01背包

初学DP,用贪心的思想想解题,可是想了一个多小时还是想不出. //在max中的两个参数f[k], 和f[k-weight[i]]+value[i]都是表示在背包容量为k时的最大价值 //f[k]是这个意思,就不用说了. //而f[k-weight[i]]+value[i]也表示背包容量为k时的最大价值是为什么呢? //首先,f[k-weight[i]]表示的是背包容量为k-weight[i]的容量,也就是说f[k-weight[i]] //表示的是容量还差weiht[i]才到k的价值,+walu

POJ 2184 01背包+负数处理

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to

Cow Exhibition POJ 2184(01背包dp)

原题 题目链接 题目分析 明显的01背包,但还是由细节需要处理,设置dp[i][j]=前i头牛中选的TF为j时最大的TS值,由于TF可能为负数因此要加一个基数使其在大于等于零,dp初始化为-1,dp[0][0]=0.更新的时候dp[i][j]=max(dp[i-1][j-f[i]]+s[i],dp[i-1][j]).观察这个dp的更新可以发现能状态压缩,当f[i]>=0时,j从大到小更新,当f[i]<0时,j从小到大更新.最后从基数往上扫dp[j]+j-基数的最大值就是答案,注意前提是dp[i

poj 1837 Balance(背包)

题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10983   Accepted: 6824 Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other

POJ之01背包系列

poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using namespace std; #define N 15010 int n,m,v[N],c[N],f[N]; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d%d",&am

Relocation POJ - 2923(01背包+状压dp)

Relocation POJ - 2923 原文地址:https://www.cnblogs.com/megadeth/p/11361007.html

POJ 1837 Balance 背包dp

点击打开链接 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11067   Accepted: 6865 Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders t

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,钩码数量