Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板~
#include <stdio.h>
#include <string.h> int a[7];
int f[100005];
int v, k; void ZeroOnePack(int cost, int weight)
{
for (int i = v; i >= cost; i--)
if (f[i - cost] + weight > f[i])
f[i] = f[i - cost] + weight;
}
void CompletePack(int cost, int weight)
{
for (int i = cost; i <= v; i++)
if (f[i - cost] + weight > f[i])
f[i] = f[i - cost] + weight;
}
void MultiplePack(int cost, int weight, int amount)
{
if (cost * amount >= v) CompletePack(cost, weight);
else {
for (int k = 1; k < amount;) {
ZeroOnePack(k * cost, k * weight);
amount -= k;
k <<= 1;
}
ZeroOnePack(amount * cost, amount * weight);
}
} int main()
{
int sum;
int cas = 0;
while (1) {
sum = 0;
for (int i = 1; i < 7; i++) {
scanf("%d", &a[i]);
sum += a[i] * i;
}
if (sum == 0) break;
if (sum % 2 == 1) {
printf("Collection #%d:\nCan't be divided.\n\n", ++cas);
continue;
} else {
v = sum / 2;
memset(f, 0, sizeof(f));
for (int i = 1; i < 7; i++)
MultiplePack(i, i, a[i]);
if (f[v] == v)
printf("Collection #%d:\nCan be divided.\n\n", ++cas);
else
printf("Collection #%d:\nCan't be divided.\n\n", ++cas);
}
}
return 0;
}
Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)的更多相关文章
- POJ 1014 Dividing(多重背包)
Dividing Description Marsha and Bill own a collection of marbles. They want to split the collectio ...
- POJ 1014 Dividing(多重背包+二进制优化)
http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...
- DFS(DP)---POJ 1014(Dividing)
原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...
- Coins(HDU 2844):一个会超时的多重背包
Coins HDU 2844 不能用最基础的多重背包模板:会超时的!!! 之后看了二进制优化了的多重背包. 就是把多重转变成01背包: 具体思路见:http://www.cnblogs.com/tt ...
- POJ 1014 Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- POJ 1014 Dividing (多重可行性背包)
题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 Dividing 背包
二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...
随机推荐
- JavaScript高级编程II
原文地址: http://www.onlamp.com/pub/a/onlamp/2007/08/23/advanced-javascript-ii.html?page=1 在前面的文章中, ...
- Qt中使用cout, cin, cerr
在Qt中,用的最多的是GUI页面,但GUI比较慢,有些小的试验不妨先用console,当然在Qt里面可以直接使用cout, cin这些. 不过可能不会兼容Qt自带的一些类型,比如QByteArra ...
- javascript內容向上不間斷滾動
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- yii中常用路径<转>
调用YII框架中jquery:Yii::app()->clientScript->registerCoreScript(‘jquery’); framework/web/js/source ...
- 利用CSS实现居中对齐
1. 文本居中 首先编写一个简单的html代码,设置一个类名为parentDiv的div对象.html代码如下: <div class="parentDiv"> 这里随 ...
- PHP Socket编程起步
让我们以一个简单的例子开始---一个接收输入字符串,处理并返回这个字符串到客户端的TCP服务.下面是相应的代码: PHP 代码: ) or die("Could not read input ...
- linux RedHat 5 更新vim.
概述: 想装 ctags,装不上.看到老外有篇日志,是在vi 7.2版本上运行.怕是vi版本的原因,于是想升级,网上升级的方法写得少,有的写的太无语了,只有他自己看得懂.这里,简单说下.搞半天了,终于 ...
- iptables 下开放ftp
这两天在给客户安装服务器时也顺便给他们使用iptables,不用不知道,一用才发现iptables还有很多东西可以学的,比如开放ftp.iptables 的filter表的INPUT链的默认策略设为了 ...
- Property type 'id<tabBarDelegate>' is incompatible with type 'id<UITabBarDelegate> _Nullable' inherited from 'UITabBar'
iOS报错:Property type 'id' is incompatible with type 'id _Nullable' inherited from 'UITabBar' 如图: 可能原因 ...
- [!] Unable to satisfy the following requirements:
出现这个问题是由于我本地Podfile文件上第三方版本太低. 解决方案就是,更新一下本地Podfile文件上的第三方版本,也就是pod update --verbose一下. 注意一下,这个命令需要很 ...