【POJ】 1014 Dividing(多重背包,优化)


【题目链接】http://poj.org/problem?id=1014


题目

Description

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input

Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , … , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line “1 0 1 2 0 0”. The maximum total number of marbles will be 20000.
The last line of the input file will be “0 0 0 0 0 0”; do not process this line.
Output

For each collection, output “Collection #k:”, where k is the number of the test case, and then either “Can be divided.” or “Can’t be divided.”.
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
Mid-Central European Regional Contest 1999


大意就是一堆体积为1-6的弹珠,给出每种个数,问能不能平分成两堆;

思路:多重背包,每一层分别转化为完全背包或01背包。


AC代码

    #include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<algorithm>#include<math.h>#include<limits.h>#include<stack>#include<queue>#define LL long longusing namespace std;int p[7]={0};int dp[220005];int v;void Zeroonepack(int cost,int value)//01背包{for(int i=v;i>=cost;i--)dp[i]=max(dp[i],dp[i-cost]+value);}void Completepack(int cost,int value)//完全背包{for(int i=cost;i<=v;i++)dp[i]=max(dp[i],dp[i-cost]+value);}void Multipack(int cost,int value,int num)//多重背包{int k;if(num*cost>=v)Completepack(cost,value);else{k=1;while(num>k){Zeroonepack(k*cost,k*value);num-=k;k*=2;}Zeroonepack(num*cost,num*value);}}int main(){int k=1,sum;while(scanf("%d%d%d%d%d%d",&p[1],&p[2],&p[3],&p[4],&p[5],&p[6])!=EOF&&!(p[1]==0&&p[2]==0&&p[3]==0&&p[4]==0&&p[5]==0&&p[6]==0)){printf("Collection #%d:\n",k++);sum=0;for(int i=1;i<7;i++){sum+=i*p[i];}if(sum%2!=0){printf("Can't be divided.\n\n");continue;}memset(dp,0,sizeof(dp));v=sum/2;for(int i=1;i<=6;i++){Multipack(i,i,p[i]);}if(dp[v]==v)//确定是否正好装满;printf("Can be divided.\n\n");else printf("Can't be divided.\n\n");}return 0;}

然后还有一个错误代码,我还没看出来为什么错,对于400 400 400 400 400 400输出了can’t


    #include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<algorithm>#include<math.h>#include<limits.h>#include<stack>#include<queue>#define LL long longusing namespace std;int p[7]={0};int f[120005];int main(){int k=1,sum;   while(scanf("%d%d%d%d%d%d",&p[1],&p[2],&p[3],&p[4],&p[5],&p[6])!=EOF&&!(p[1]==0&&p[2]==0&&p[3]==0&&p[4]==0&&p[5]==0&&p[6]==0)){printf("Collection #%d:\n",k++);sum=0;int v;for(int i=1;i<7;i++){sum+=i*p[i];}if(sum%2!=0){printf("Can't be divided.\n");continue;}memset(f,0,sizeof(f));v=sum/2;for(int i=1;i<=6;i++){for(int vv=v;vv>0;vv--){for(int j=1;j<=p[i];j*=2){if(j*i<=vv) f[vv]=max(f[vv],f[vv-j*i]+j*i);}}}if(f[v]==v)printf("Can be divided.\n");else printf("Can't be divided.\n");}return 0;}

【POJ】 1014 Dividing(多重背包,优化)相关推荐

  1. POJ 1014 Dividing【多重背包+二进制优化】

    大意: 价值1, 2, 3, --, 6的物品分别a1, a2, --, a5, a6件 问能否把这些物品分成两份,使其具有相同的价值(所有物品必须全部用上) 分析: 给个物品有多件,即多重背包 只要 ...

  2. POJ 1014 Dividing(多重背包 + 倍增优化)

    题意: 求一种划分方案使结果最公平. 思路: 多重背包,然后把物品分为 2^0, 2^1, ... , 2^k,... 等. #include <iostream> #include &l ...

  3. (step3.3) hdu 1059(Dividing——多重背包)

    题目大意:分别给出价值为1~6的石头的数量.问能否将这些石头等价值平分... 解题思路:多重背包 1)多重背包的典型描述是这样的:给出n种物品,背包的容量为V.每种物品的可用数量为num[i],所占体 ...

  4. [poj 1014]Dividing的DFS解法解读和DP解法

    转载来源: http://blog.csdn.net/lyy289065406/article/details/6661449 这道题比较特殊,用DFS也是对的,而且可以进行优化,即使直接n[i]-- ...

  5. WikiOI 3269 混合背包 (动规+多重背包优化)

    3269 混合背包 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 背包体积为V ,给出N个物品,每个物品 ...

  6. POJ - 2392 朴素多重背包 + 贪心 WA与AC代码细节分析

    我们先来看下普通的朴素多重背包(拆成01背包求解) n种物品,背包大小w,每种物品重量 wi,价值 vi,个数 ci dp[j] 表示 大小为 j 的背包含有的最大价值,即 物品重量和 小于等于 j ...

  7. POJ 1014 Dividing 背包

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

  8. POJ 1014: Dividing

    写在前面: 本题非常有意思, 因此我在编程前后和撰写本文前, 在网上查询并阅读了大量关于本题的解题报告. 当中有两种似是而非但提交后却可以 AC 的解法引起了我的兴趣, 一是原作者宣称的 " ...

  9. HDU 1059 Dividing 多重背包

    这题想了想用母函数应该也可以做,不过得考虑一个细节,就是加起来的总和是否为奇数,我找了好久一直没找出错误,原来是没考虑奇数..  fuck  这种问题我怎么能不考虑奇数呢.....  啊 啊 啊啊 啊 ...

  10. POJ 1014 Dividing

    套模板+填参数=AC POJ还是用C++提交靠谱 Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 51256 A ...

最新文章

  1. 一般关于大宗商品的供需关系相关的数据网址有哪些?
  2. python到底怎么学-学 Python 到底能帮你解决什么问题 ?
  3. mysql性能调整三板斧
  4. Android ListView选中项居中放大(使用上下键控制,非触屏)
  5. 倾斜摄影实景三维建模效果不好?可能有这些原因!
  6. [导入]Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]...
  7. Day 1: Introduction to Deep Learning
  8. 解决lenovo V470 安装win8 无法重启、关机故障
  9. OSPF之Stub区域
  10. 元宇宙专题001 | 他们居然将元宇宙和心理学写到一起了
  11. Matlab中FrechetDistance方法实现---比较两条曲线的相似性,并绘制曲线
  12. android 根据宽度调整字体大小,android 字体大小 根据分辨率 自动调整
  13. open-能连接,但无法访问内网的问题
  14. 催收行业再现“暴力(利)”
  15. SEO培训联盟排名掉的原因:宋星博客?
  16. (JDK8)jdk-8u201-windows-x64 安装及其环境变量配置
  17. YDOOK:Pytorch教程:tensor 张量内各个值同时相加一个数
  18. VMware注册问题
  19. wps excel 链接url转图片工具
  20. 关于浏览器自动安装Screenshot Pro拓展的问题

热门文章

  1. c语言输出漏斗图形7层,ECharts 教程 漏斗图属性与实例介绍 - 闪电教程JSRUN
  2. vue实现图书管理案例
  3. 免费计算机论文 阅读,关于计算机的毕业论文
  4. 【转】用IDCNN和CRF做端到端的中文实体识别
  5. 云服务器和真实服务器,个人网站主机选择原则 看配置也要看是不是有助于优化...
  6. Filter为什么会在一次请求执行多次doFilter?
  7. 史上最全 | 基于深度学习的3D分割综述(RGB-D/点云/体素/多目)
  8. 无向图的关联矩阵JAVA_图的矩阵表示无向图及有向图的关联矩阵.doc
  9. mysql握手_详细介绍mysql 协议的服务端握手包及对其解析
  10. Hopefield神经网络