POJ 1014 Dividing 多重背包
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 63980 | Accepted: 16591 |
Description
Input
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided. Collection #2:
Can be divided.
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int sum;
int num[7], dp[60000 + 60];
template<class T > inline T getMax(const T &a, const T &b)
{
return a > b ? a : b;
} void ZeroOnePack(int cost, int weight, int V)
{
for (int i = V; i >= cost; i--) {
dp[i] = getMax(dp[i], dp[i - cost] + weight);
}
} void CompletePack(int cost, int weight, int V)
{
for (int i = cost; i <= V; i++) {
dp[i] = getMax(dp[i], dp[i - cost] + weight);
}
} void MultiPack(int cost, int weight, int V, int amount)
{
if (cost * amount >= V) {
CompletePack(cost, weight, V);
return;
}
int k = 1;
while (k < amount) {
ZeroOnePack(cost * k, weight * k, V);
amount -= k;
k *= 2;
}
ZeroOnePack(cost * amount, weight * amount, V);
} int main()
{
int t = 1;
while (~scanf("%d", &num[1])) {
sum = num[1];
for (int i = 2; i <= 6; i++) {
scanf("%d", &num[i]);
sum += num[i] * i;
}
if (num[1] + num[2] + num[3] + num[4] + num[5] + num[6] == 0) break;
printf("Collection #%d:\n", t++);
if (sum % 2) {
puts("Can't be divided.\n");
continue;
}
sum >>= 1;
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= 6; i++) {
MultiPack(i, i, sum, num[i]);
}
if (dp[sum] != sum) {
puts("Can't be divided.\n");
} else
puts("Can be divided.\n");
}
return 0;
}
POJ 1014 Dividing 多重背包的更多相关文章
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 Dividing (多重可行性背包)
题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...
- POJ 1014 Dividing(多重背包+二进制优化)
http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...
- POJ 1014 Dividing(多重背包)
Dividing Description Marsha and Bill own a collection of marbles. They want to split the collectio ...
- DFS(DP)---POJ 1014(Dividing)
原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- POJ 2392【多重背包】
题意: k个块,给出每个块的高度hi,数量ci,不能超过的高度: 求这些块可以组成的最大高度一个. 思路: 大致可看这个题是一个背包,背包的承重是高度. 对于每个物品,有他的价值是高度,还有限定的数量 ...
随机推荐
- mao/reduce实现求平均值
import java.io.*; import java.util.*; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io. ...
- HTTPS、SSL与数字证书介绍
在互联网安全通信方式上,目前用的最多的就是https配合ssl和数字证书来保证传输和认证安全了.本文追本溯源围绕这个模式谈一谈. 名词解释 HTTPS:在HTTP(超文本传输协议)基础上提出的一种安全 ...
- windows下redis的安装配置和php扩展使用phpredis
1. 首先安装先下载redis数据库 下载地址: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload 目前是2.02 ...
- iOS性能优化中的离屏渲染
GPU屏幕渲染有以下两种方式: On-Screen Rendering意为当前屏幕渲染,指的是GPU的渲染操作是在当前用于显示的屏幕缓冲区中进行. Off-Screen Rendering意为离屏渲染 ...
- 【转】Android应用开发allowBackup敏感信息泄露的一点反思
转载:http://blog.csdn.net/yanbober/article/details/46417531 1 背景 其实这篇文章可能有些小题大作,但回过头想想还是很有必要的,有点阴沟里翻船的 ...
- adb概览及协议參考
原文:https://github.com/android/platform_system_core/blob/master/adb/OVERVIEW.TXT) Implementation note ...
- IOPS QPS TPS
杨奇龙: http://blog.itpub.net/22664653/viewspace-767265/ http://blog.itpub.net/22664653/viewspace-76726 ...
- qemu 的方式安装debian 模拟powerpc
http://bbs.pediy.com/showthread.php?p=1424746http://www.ibm.com/developerworks/cn/linux/l-qemu/ 线总结下 ...
- &&与&
if((2>1)&&(4>3))System.out.printf("两边都是true"); else System.out.println(&qu ...
- 墙裂推荐 iOS 资源大全
这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...