http://acm.hdu.edu.cn/showproblem.php?pid=1059

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29901    Accepted Submission(s): 8501

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.
 
Source
多重背包加二进制优化问题
背包容量为物品总价值的一半,如果总价值为奇数则肯定不可以分
#include<bits/stdc++.h>
using namespace std;
#define max_v 120000
#define max_n 7
int num[max_n],f[max_v],v[max_n]={0,1,2,3,4,5,6};
void ZeroOnePack(int cost,int weight,int c)
{
for(int v=c;v>=cost;v--)
{
f[v]=max(f[v],f[v-cost]+weight);
}
}
void CompletePack(int cost,int weight,int c)
{
for(int v=cost;v<=c;v++)
{
f[v]=max(f[v],f[v-cost]+weight);
}
}
void MultiplePack(int cost,int weight,int amount,int c)
{
if(cost*amount>=c)
{
CompletePack(cost,weight,c);
return ;
}
int k=1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight,c);
amount-=k;
k*=2;
}
ZeroOnePack(amount*cost,amount*weight,c);
}
int main()
{
int t=0;
while(1)
{
int sum=0,flag=0;
for(int i=1;i<max_n;i++)
{
scanf("%d",&num[i]);
sum=sum+i*num[i];
}
if(sum==0)
break;
if(sum%2==1)
{
printf("Collection #%d:\nCan't be divided.\n\n",++t);
continue;
}
sum=sum/2;
memset(f,0,sizeof(f));
for(int i=1;i<max_n;i++)
{
MultiplePack(v[i],v[i],num[i],sum);
}
//printf("%d\n",f[sum]);
if(f[sum]==sum)
{
flag=1;
}
if(flag==1)
{
printf("Collection #%d:\nCan be divided.\n\n",++t);
}else
{
printf("Collection #%d:\nCan't be divided.\n\n",++t);
}
}
return 0;
}

  

HDU 1059(多重背包加二进制优化)的更多相关文章

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

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

  2. hdu 1059 (多重背包) Dividing

    这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...

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

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

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

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

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

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

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

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

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

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

  8. hdu 1059 多重背包

    题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 #include<stdio ...

  9. Dividing (hdu 1059 多重背包)

    Dividing Sample Input 1 0 1 2 0 0 价值为1,2,3,4,5,6的物品数目分别为 1 0 1 2 0 0,求能否将这些物品按价值分为两堆,转化为多重背包.1 0 0 0 ...

随机推荐

  1. laravel开发之-(1)数据库链接测试

    composer安装成功后,网站建设的操作记录: 一.修改默认首页.伪静态配置文件 1.将serve.php改为index.php 2.public文件夹下的.htaccess文件复制到根目录下 二. ...

  2. SQL-OVER与四种排名函数:ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE()

    1 SELECT orderid,custid,val, ROW_NUMBER() OVER(ORDER BY val) AS rownum, RANK() OVER(ORDER BY val) AS ...

  3. centos虚拟机网卡配置

    连接模式为NAT

  4. Xshell启动时显示丢失MSVCP110.dll

    重装系统,装完Xshell5启动时,出现丢失MSVCP110.dll文件 这种情况不要相信网上所说的什么下载“MSVCP110.dll”文件或者下载微软的vcredist 2012 这样没用 正确的姿 ...

  5. kettle 合并记录

    转自: http://blog.itpub.net/post/37422/464323 看到别人的脚本用到 合并记录 步骤,学下下. 该步骤用于将两个不同来源的数据合并,这两个来源的数据分别为旧数据和 ...

  6. iOS设计模式 - 桥接

    iOS设计模式 - 桥接 示意图 说明 1. 桥接模式为把抽象层次结构从实现中分离出来,使其可以独立变更,抽象层定义了供客户端使用的上层抽象接口,实现层次结构定义了供抽象层次使用的底层接口,实现类的引 ...

  7. 转载:什么才是真正的 RESTful 架构

    What? Wikipedia: 表征性状态传输(英文:Representational State Transfer,简称REST)是Roy Fielding博士于2000年在他的博士论文中提出来的 ...

  8. 在 Windows 容器中使用 gMSA

    前不久给公司搭测试环境,其中涉及到了某组件在容器中使用 kerberos 身份验证连接 SQL Server 数据库的问题. Windows 容器本身并不能加入域,但可以通过 gMSA 运行容器使容器 ...

  9. Spring学习---Spring中利用jackson进行JSON转换

    Spring中利用jackson进行JSON转换 import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExce ...

  10. MySQL -Naivacat工具与pymysql模块

    Navicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库. 官网下载:https ...