题意

有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是(即没有物品了),这程序结束,总物品的总个数不超过20000

思路

裸的多重可行性背包,设dp[i]表示容量为i是否可装。状态设计看代码吧。

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;

int num[7];
bool dp[130000];
void zero_one_pack(int num, int V){
for (int i = V; i >= num; i --){
if (dp[i]) continue;
if (dp[i-num])
dp[i] = true;
}
return ;
}
void complete_pack(int num, int V){
for (int i = num; i <= V; i ++){
if (dp[i]) continue;
if (dp[i-num])
dp[i] = true;
}
return ;
}
void multi_pack(int num, int amount, int V){
if (num * amount >= V){
complete_pack(num, V);
}
int k = 1;
while(amount > k){
zero_one_pack(k*num, V);
amount -= k;
k <<= 1;
}
zero_one_pack(amount*num, V);
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int t = 1;
while(scanf("%d %d %d %d %d %d", &num[1], &num[2], &num[3], &num[4], &num[5], &num[6])){
if (num[1] + num[2] + num[3] + num[4] + num[5] + num[6] == 0){
break;
}
printf("Collection #%d:\n", t);
int sum = 0;
for (int i = 1; i <= 6; i ++){
sum += num[i] * i;
}
if (sum % 2 != 0){
puts("Can't be divided.");
puts("");
t ++;
continue;
}
sum /= 2;
MEM(dp, false);
dp[0] = true;
for (int i = 1; i <= 6; i ++){
multi_pack(i, num[i], sum);
}
if (dp[sum]){
puts("Can be divided.");
}
else{
puts("Can't be divided.");
}
puts("");
t ++;
}
return 0;
}

[/cpp]

POJ 1014 Dividing (多重可行性背包)的更多相关文章

  1. 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 ...

  2. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...

  3. POJ 1014 Dividing(多重背包, 倍增优化)

    Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...

  4. POJ 1014 Dividing(多重背包+二进制优化)

    http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...

  5. POJ 1014 Dividing(多重背包)

    Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collectio ...

  6. POJ 1014 Dividing 背包

    二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...

  7. DFS(DP)---POJ 1014(Dividing)

    原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...

  8. POJ 1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...

  9. POJ 1014 Dividing(入门例题一)

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description Marsha and Bill own ...

随机推荐

  1. linux一键安装包

  2. Windows10 蓝屏 DRIVER_IRQL_NOT_LESS_OR_EQUAL (vfilter.sys)的可能解决方法

    早上我的笔记本从休眠中开机的时候突然出现了蓝屏,这个蓝屏在前几天出现过了.两次提示的终止代码都一样.我的笔记本型号是DELL XPS15 9560 我的笔记本配置: 类别 型号 内存 16GB DDR ...

  3. 写Java代码的一些小技巧

    写Java代码有三年多了,遇到过很多坑,也有一些小小的心得.特地分享出来供各位学习交流.这些技巧主要涉及谷歌Guava工具类的使用.Java 8新特性的使用.DSL风格开发.代码封装等技巧. 一.nu ...

  4. chrome浏览器新建标签打开页面【学习笔记】

    按照下面方法进行设置即可

  5. hdu Naive Operations 线段树

    题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...

  6. FieldOffset

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.G ...

  7. 论文笔记:Mastering the game of Go with deep neural networks and tree search

    Mastering the game of Go with deep neural networks and tree search Nature 2015  这是本人论文笔记系列第二篇 Nature ...

  8. 论文笔记——SQUEEZENET ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE

    论文地址:https://arxiv.org/abs/1602.07360 模型地址:https://github.com/DeepScale/SqueezeNet 1. 论文思想 提出一种新的卷积组 ...

  9. [异常记录-12]Web Deploy部署:未能连接到远程计算机,请确保在远程计算机上安装了 Web Deploy 并启动了所需的进程("Web Management Service")

    Web Deploy 安装 请参考:图文详解远程部署ASP.NET MVC 5项目 如此安装后还不行,  可以在卸载后重新安装 Web Deploy 时,不要选那个经典还是典型的安装选项,选自定义安装 ...

  10. Oracle监听服务

    Oracle数据库中的主要用户及其作用 No. 用户名 默认密码 描述 1 sys change_on_install 数据库的超级管理员 2 system manager 数据库的普通管理员 3 s ...