POJ1837Balance(分组背包)

Balance

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11042   Accepted: 6855

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

题目大意:有一把称,上有C个沟子在不同的位置,可挂法码,有G个法码可以挂,问把全部法码挂上去有多少种平衡方法。

解题:因为必须把所有的法码都要挂上去,去以可以用分组背包,dp[gn][v+H[i]*G[gn]]的组成是用第gn个法码,第i个沟子去跟dp[gn-1][v]组合。

PS:如果碰到的题目说,必须把所有的东西都要用上就可以用分组背包。

#include<stdio.h>
#include<string.h>

int dp[25][15005];

int main()
{
    int hn,gn,H[25],G[25],mid0=7500;//表示整体平移的多少,把负数变正数,0则变成mid0
    while(scanf("%d%d",&hn,&gn)>0)
    {
        for(int i=1;i<=hn; i++)
            scanf("%d",&H[i]);
        for(int i=1;i<=gn; i++)
            scanf("%d",&G[i]);

        memset(dp,0,sizeof(dp));
        dp[0][mid0]=1;//挂0个物品平衡
        for(int i=0;i<gn; i++)
            for(int v=0;v<=15000;v++)
            if(dp[i][v])//称上挂了前i个物品组成v的状态有多少种
            {
                for(int j=1;j<=hn; j++)
                    if(v+H[j]*G[i+1]>=0&&v+H[j]*G[i+1]<=15000)
                        dp[i+1][v+H[j]*G[i+1]]+=dp[i][v];
            }

        printf("%d\n",dp[gn][mid0]);
    }
}
时间: 2024-07-30 19:06:46

POJ1837Balance(分组背包)的相关文章

VijosP1250:分组背包

背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n个物品,每个物品都有一个价值Pi和重量Wi,但是有些物品放在一起会爆炸,并且爆炸具有传递性.(a和b会爆炸.b和c会爆炸则a和c会爆炸)机器人们可不想因此损失自己好不容易从Wind那里敲诈来的装备,于是它们想知道在能力范围内,它们最多可以拿多少价值的物品. 你能帮助它们吗? 格式 输入格式 每组测试数据第1行为n,Wmax,

HDU-1011 Starship Troopers (树形DP+分组背包)

题目大意:给一棵有根带点权树,并且给出容量.求在不超过容量下的最大权值.前提是选完父节点才能选子节点. 题目分析:树上的分组背包. ps:特判m为0时的情况. 代码如下: # include<iostream> # include<cstdio> # include<vector> # include<cstring> # include<algorithm> using namespace std; const int N=105; const

HDU 4003 Find Metal Mineral (树形DP,树形分组背包,经典)

题意:给定一棵树图,n个节点,有边权,要派k<11个机器人从节点s出发,遍历所有的点,每当1只机器人经过1条边时就会花费该边的边权,边是可重复走的.问遍历完所有点的最小花费? 思路: 非常经典,首先需要知道一点,才能往下推理.就是“如果在t点派c个机器人往孩子u,那么最多只有1个机器人能走会回来到t,否则花费总是不划算的”. 稍微证明一下: (1)假设派1个机器人往u,逛一圈回到u的父亲t,花费v= 子树u的边权和*2 + e(t,u)*2.若机器人不要了,那花费肯定比v还要少. (2)假设派2

HDU 1712 ACboy needs your help-dp-(分组背包模型)

题意:n门课程用m天来学习,每门课用不同的天数来学习会有不同的学分,求能得到的最大的学分 分析:第一次接触分组背包.分组背包的模型就是不同的物品有不同的花费和价值,求在规定花费内能得到的最大的价值,这前面跟以前的背包最大的不同是物品分为几组,每组内的物品最多只能选一种:dp[i][j]表示前i组花费j能得到的最大的价值,不过实际在做的时候用一维数组就可以了 公式: for 组i for 花费j (从大到小) for 组内物品k if(j>=c[k]) dp[j]=max(dp[j],dp[j-c

hdu 1712 分组背包

背景:1Y,01背包多加了一个挑选循环而已. 分组背包的典型描述:对于很多背包,把它分为k个组,每个组内的组员是相互冲突的,所以只能选择一个. 我的代码: #include<cstdio> #include<iostream> #include<cstring> using namespace std; int main(void){ int n,m; while(scanf("%d%d",&n,&m),n*n+m*m){ int c

hdu4003 树形dp+分组背包

http://acm.hdu.edu.cn/showproblem.php?pid=4003 Problem Description Humans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots on

【HDU1712】ACboy needs your help(分组背包)

将背包九讲往后看了看,学习了一下分组背包.来做几道入门题,试试手. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 #inc

洛谷——P1757 通天之分组背包

https://www.luogu.org/problem/show?pid=1757#sub 题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品相互冲突,现在,他想知道最大的利用价值是多少. 输入输出格式 输入格式: 两个数m,n,表示一共有n件物品,总重量为m 接下来n行,每行3个数ai,bi,ci,表示物品的重量,利用价值,所属组数 输出格式: 一个数,最大的利用价值

D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses 分组背包模板题

http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判断. 这样是不对的.因为这样使得同一组里面可能选择了两次. 3 0 2 1 2 3 1 1 3 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include &