题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子(每个钩子有相对于中心的距离,左负右正),有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。

将每个砝码看作一组,组内各个物品的体积为每个挂钩与该砝码形成的力矩,背包总体积严格为0,这便是分组背包计数问题(特殊点:每一组必须出一个物品,而不是至多出一个物品)。由于c++不允许负的数组下标,所以每次更新时,j要加上offsetJ。

实现分组背包计数问题时,可以用填表法(找以前节点求自己值)(DP1)或刷表法(找以后节点更新以后值)(DP2)。由于刷表法时,如果DP[i][j]==0,可以跳过,所以节省时间。

注意:

  • 不可以用一维数组倒序循环来表示DP数组,因为j-objV可能比j还大
  • 同理,每次判断时,不是j-objV>=0,0代表天平的支点而不是左端点。所以应当为j-objV>=minJ。
#include <cstdio>
#include <cstring>
#include <cstdarg>
using namespace std; const int MAX_V = , MAX_OBJ = , MAX_HOOP = ;
const int Plus = ;
#define Sub(x) x+offsetJ
int TotHoop;
int Len[MAX_HOOP], W[MAX_OBJ]; void _printf(char *format, ...)
{
#ifdef _DEBUG
va_list(args);
va_start(args, format);
vprintf(format, args);
va_end(args);
#endif
} int DP1(int totHoop, int totDev, int *_w, int *_len)
{
int minJ = , maxJ = , wSum = , offsetJ, objV = ;
static int DP[MAX_OBJ][MAX_V];
for (int dev = ; dev <= totDev; dev++)
wSum += _w[dev];
for (int hoop = ; hoop <= totHoop; hoop++)
_len[hoop] > ? maxJ += _len[hoop] * wSum : minJ += _len[hoop] * wSum;
offsetJ = -minJ;
DP[][offsetJ] = ;
_printf("+ offsetJ %d maxJ %d\n", +offsetJ, maxJ);
for (int i = ; i <= totDev; i++)
for (int j = minJ; j <= maxJ; j++)
for (int hoop = ; hoop <= totHoop; hoop++)
{
objV = _w[i] * _len[hoop];
if (j - objV >= minJ && j - objV <= maxJ)
{
DP[i][j + offsetJ] += DP[(i - )][j + offsetJ - objV];
_printf("%d=DP[%d][%d] += DP[%d][%d]=%d\n", DP[i][j + offsetJ], i, j, i - , j - _w[i] * _len[hoop], DP[i - ][j - _w[i] * _len[hoop] + offsetJ]);
}
}
return DP[totDev][offsetJ];
} int DP2(int totHoop, int totDev, int *_w, int *_len)
{
int minJ = , maxJ = , wSum = , offsetJ, objV = ;
static int DP[MAX_OBJ][MAX_V];
for (int dev = ; dev <= totDev; dev++)
wSum += _w[dev];
for (int hoop = ; hoop <= totHoop; hoop++)
_len[hoop] > ? maxJ += _len[hoop] * wSum : minJ += _len[hoop] * wSum;
offsetJ = -minJ;
DP[][offsetJ] = ;
_printf("+ offsetJ %d maxJ %d\n", + offsetJ, maxJ);
for (int i = ; i < totDev; i++)
for (int j = minJ; j <= maxJ; j++)
if (DP[i][j+ offsetJ])
for (int hoop = ; hoop <= totHoop; hoop++)
{
objV = _w[i+] * _len[hoop];
DP[i + ][j + objV + offsetJ] += DP[i][j + offsetJ];
_printf("%d=DP[%d][%d] += DP[%d][%d]=%d\n", DP[i+][j+objV+ offsetJ], i+, j+objV+ offsetJ, i, j+ offsetJ , DP[i][j + offsetJ]);
}
return DP[totDev][offsetJ];
} int main()
{
#ifdef _DEBUG
freopen("c:\\noi\\source\\input.txt", "r", stdin);
#endif
int totDevice, vCnt = , totV = , maxV = , minV = ;
scanf("%d%d", &TotHoop, &totDevice);
for (int i = ; i <= TotHoop; i++)
scanf("%d", i + Len);
for (int i = ; i <= totDevice; i++)
scanf("%d", i + W);
printf("%d\n", DP2(TotHoop, totDevice, W, Len));
return ;
}

POJ1837 Balance 背包的更多相关文章

  1. POJ1837 Balance[分组背包]

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13717   Accepted: 8616 Descript ...

  2. HDU 5616 Jam's balance 背包DP

    Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...

  3. poj 1837 Balance(背包)

    题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  4. poj1837 Balance

    Balance  POJ - 1837 题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 其中可以把天枰看做一个以x轴0点 ...

  5. poj1837【背包】

    题意: 有一根杆子,给出一些杆子上的位置,位置上能放重物,再给出一些重物的重量. 重物都需要被使用,但是位置不一定都要用到. 问你能有多少种方法让这个杆子平衡. 思路: 在位置上是0/1背包思想,取或 ...

  6. poj1837 01背包(雾

    Description A train has a locomotive that pulls the train with its many passenger coaches. If the lo ...

  7. POJ1837 Balance(DP)

    POJ1837http://poj.org/problem?id=1837 题目大意就是说有一个称上有C个挂钩,告诉你每个挂钩的位置,现在有G个重物,求是之平衡的方法数. 转化一下:DP[i][j]表 ...

  8. poj 01背包

    首先我是按这篇文章来确定题目的. poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<algo ...

  9. POJ之01背包系列

    poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...

随机推荐

  1. js数据管理的思考

    最近要做一个农场项目,涉及到很多js数据管理的需求,这里也做下总结,不断的总结,再修正内容,也是快速进步的方法. 数据管理几个方面考虑: * 设置(更新)字段值 * 检索,根据id, index, 属 ...

  2. Spring Boot (15) pom.xml设置

    继承spring-boot-parent 要成为一个spring boot项目,首先就必须在pom.xml中继承spring-boot-starter-parent,同时制定其版本 <paren ...

  3. Laravel5.1 学习笔记2, 路由

    安装的说明请看文档, laravel 安装 #基本路由 你将在 app/Http/routes.php 文件定义大部分路由, 这些路由将被App\Providers\RouteServiceProvi ...

  4. P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...

  5. 【Oracle】Rman备份策略

    1. 查看可设置参数 RMAN> show all; RMAN configuration parameters for database with db_unique_name DRZ are ...

  6. Quartz+Topshelf 作业

    小记: 引用Quartz.Topshelf.Topshelf.Quartz 使用方法: http://www.cnblogs.com/mushroom/p/4952461.html http://ww ...

  7. (转)淘淘商城系列——Solr集群搭建

    http://blog.csdn.net/yerenyuan_pku/article/details/72957201 我们之前做的搜索使用的是Solr的单机版来实现的,正是由于我们现在商品数据量不多 ...

  8. 怎么从传统的盒子思想转为Flex 布局(css)

    前端进化很快,总是有新的技术出来,开始可能有些人用惯了盒子模型的思想 依赖 display属性 + position属性 + float属性.这三大件.它对于那些特殊布局非常不方便 我们就来看看Fle ...

  9. PAT_A1149#Dangerous Goods Packaging

    Source: PAT A1149 Dangerous Goods Packaging (25 分) Description: When shipping goods with containers, ...

  10. 【剑指Offer】12、数值的整数次方

      题目描述:   给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.   解题思路:   本题看似比较简单,是一个简单的指数运算,但需要完 ...