Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8755    Accepted Submission(s): 2374

Problem 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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., 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 colletcion, 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.
 
题意:把六件物品,按价值平分;

解题心得:
1、其实这就是要给多重背包的问题,但是按照普通的做法那肯定是会超时的。需要优化,也就是二进制优化。
2、先说说二进制优化,这个题的二进制优化,就是将一个数改成很多个二的倍数的的数的和(例如:100可以变成2+4+8+16+32+37,这个37是剩下的可以不做处理),然后将O(n)的复杂度优化成O(logn)的复杂度。不太明白二进制问题的可以去看看快速幂的算法(快速幂入门)。实现方法主要看代码


自己在做这个题的时候的代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 6e5;
bool dp[maxn];
typedef struct
{
int va;
int num;
} Res;
int main()
{
int T = 1;
Res res[10];
for(int i=1; i<=6; i++)
res[i].va = i;
while(scanf("%d%d%d%d%d%d",&res[1].num,&res[2].num,&res[3].num,&res[4].num,&res[5].num,&res[6].num) != EOF)
{
memset(dp,0,sizeof(dp));
dp[0] = true;
int Return = 0,sum = 0;
for(int i=1; i<=6; i++)
{
Return += res[i].num;//全是0跳出
sum += res[i].va * res[i].num;
}
if(!Return)
break; printf("Collection #%d:\n",T++);
if(sum % 2)//单数直接排除
{
printf("Can't be divided.\n\n");
continue;
} sum /= 2;
for(int i=1; i<=6; i++)
{
int cnt = 0;
for(int k=1; k<=res[i].num; k*=2)//k每次乘以2
{
cnt = k*res[i].va;
for(int j=sum; j>=cnt; j--)
{
if(dp[j-cnt])
dp[j] = true;
}
res[i].num -= k;//减去二进制之后的就是剩下的
} if(cnt)//当res[i].num != 0
{
cnt = res[i].va * res[i].num;//将二进制处理之后剩下的一起处理
for(int j=sum; j>=cnt; j--)
{
if(dp[j-cnt])
dp[j] = true;
}
}
} if(dp[sum])
printf("Can be divided.\n");
else
printf("Can't be divided.\n");
printf("\n");
}
}


大神的代码(这个更加的容易理解)

大概I的意思就是将多重背包看作完全背包或者0-1背包来进行优化(多重背包的实质就是0-1背包,都是一样一样的),优化部分主要就是0-1背包部分,注意在每次调用0-1背包的时候的参数传递


#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXV 60010 using namespace std; int d[MAXV],V; void bag01(int c,int w)//01背包
{
int i;
for(i=V;i>=c;i--)
{
if(d[i]<d[i-c]+w)
{
d[i]=d[i-c]+w;
}
}
} void bagall(int c,int w)//完全背包
{
int i;
for(i=c;i<=V;i++)
{
if(d[i]<d[i-c]+w)
{
d[i]=d[i-c]+w;
}
}
} void multbag(int c,int w,int n)//多重背包
{
if(c*n>=V)//当某一项的价值总和比需要求的价值综合还更大时可以看作完全背包问题
{
bagall(c,w);return ;
}
int k=1;
while(k<=n)//不然就看做很多个0-1背包问题的总和
{
bag01(k*c,k*w);
n=n-k;
k=k*2;
}
bag01(n*c,n*w);
} int main()
{
int n[6],sumv,i,k=1;
while(cin>>n[0]>>n[1]>>n[2]>>n[3]>>n[4]>>n[5],n[0]+n[1]+n[2]+n[3]+n[4]+n[5])
{
memset(d,0,sizeof(d));
sumv=n[0]+n[1]*2+n[2]*3+n[3]*4+n[4]*5+n[5]*6;
if(sumv%2==1)
{
printf("Collection #%d:\nCan't be divided.\n\n",k++);
continue;
}
V=sumv/2;
for(i=0;i<6;i++)
{
if(n[i]) multbag(i+1,i+1,n[i]);
}
if(V==d[V]) printf("Collection #%d:\nCan be divided.\n\n",k++);
else printf("Collection #%d:\nCan't be divided.\n\n",k++);
}
return 0;
}





动态规划:HDU1059-Dividing(多重背包问题的二进制优化)的更多相关文章

  1. 51nod 多重背包问题(二进制优化)

    有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...

  2. 51 Nod 1086 多重背包问题(二进制优化)

    1086 背包问题 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放 ...

  3. 动态规划:HDU2844-Coins(多重背包的二进制优化)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  4. HDU 1059(多重背包加二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Me ...

  5. HDU-2844 Coins 多重背包 物品数量二进制优化

    题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...

  6. luogu||P1776||宝物筛选||多重背包||dp||二进制优化

    题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...

  7. CodeForces922E DP//多重背包的二进制优化

    https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...

  8. dp之多重背包(二进制优化)

    void solve(int v,int w,int c){    int count=0;    for(int k=1;k<=c;k<<=1)    {        val[c ...

  9. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

随机推荐

  1. pygame 使用

    模块概况 display image event key mouse font 类概况 Rect: 返回的矩阵区域(图片) Surface: 可以看做是一个贴图, 它就是来显示的 display(与显 ...

  2. Java使用Zxing生成、解析二维码工具类

    Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法. 1.二维码的生成 (1).将Zxing-core.jar 包加入到classpath下. (2). ...

  3. XShell远程连接本地虚机

    有很多朋友在自己电脑上部署完成了虚机,但是不知道怎么去用工具连接自己的虚机,下面给大家讲一下大概的步骤,不足之处敬请指正!! 1.打开我们的虚拟机平台,登录虚机 2.远程那肯定要知道虚机的IP地址,在 ...

  4. 谷歌插件 JSON-Handle

    JSON-Handle http://jsonhandle.sinaapp.com/ 点击下载 插件下载后,在浏览器输入:chrome://extensions/ 将下载后的文件拖入 chrome浏览 ...

  5. js重载的实现

    在JavaScript高级程序设计书中看到 ECMAScript函数中不能想传统意义上那样实现重载.而在其他语句中(Java)中,可以为一个函数编写两个定义,只要两个定义的签名(接受的参 数的类型和数 ...

  6. centos 安装 freeswitch,开启与关闭

    ---恢复内容开始--- 官网说明地址 :https://freeswitch.org/confluence/display/FREESWITCH/CentOS+7+and+RHEL+7 1.获取源码 ...

  7. uvm_svcmd_dpi——DPI在UVM中的实现(二)

    UVM中有需要从cmmand line 输入参数的需求,所有uvm_svcmd_dpi.svh和uvm_svcmd_dpi.cc 文件就是实现功能. uvm_svcmd_dpi.svh的源代码如下,我 ...

  8. Linux文件的三个时间属性(Atime,Mtime,Ctime)

    Linux下,一个文件有三种时间,分别是: 访问时间:atime 修改时间:mtime 状态时间:ctime 访问时间:对文件进行一次读操作,它的访问时间就会改变.例如像:cat.more等操作,但是 ...

  9. 数据类型 -- uint32_t 类型

    整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类型 ...

  10. 05、Win7上openSSH的安装与配置

    05.Win7上openSSH的安装与配置 1.概述 linux上的ssh命令在网络通信场景下非常方便.现在windows也支持ssh方式和远程主机进行访问.如果只是使用ssh简单的访问功能,就需要很 ...