HDU 1059(多重背包加二进制优化)
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
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.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output a blank line after each test case.
Can't be divided.
Can be divided.
#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(多重背包加二进制优化)的更多相关文章
- 动态规划:HDU2844-Coins(多重背包的二进制优化)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 1059 (多重背包) Dividing
这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...
- dp之多重背包(二进制优化)
void solve(int v,int w,int c){ int count=0; for(int k=1;k<=c;k<<=1) { val[c ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- luogu||P1776||宝物筛选||多重背包||dp||二进制优化
题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...
- CodeForces922E DP//多重背包的二进制优化
https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...
- HDU-2844 Coins 多重背包 物品数量二进制优化
题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...
- hdu 1059 多重背包
题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 #include<stdio ...
- 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 ...
随机推荐
- hadoop fs -put localfile . 时出现如下错误: could only be replicated to 0 nodes, instead of 1
hadoop fs -put localfile . 时出现如下错误:could only be replicated to 0 nodes, instead of 1网友的说法: 这个问题是由于没有 ...
- vim 配置文件.vimrc,高亮+自动缩进+行号+折叠+优化
将一下代码copy到 用户目录下 新建文件为 .vimrc保存即可生效: 如果想所有用户生效 请修改 /etc/vimrc (建议先cp一份)"====================== ...
- 从BASE理论到CAP理论
BASE理论面向的是大型高可用可扩展的分布式系统,和传统事务的CID特性是相反的,它完全不同于ACID的强一致性模型,而是提出通过牺牲强一致性来获得可用性,并允许数据在一段时间内是不一致的,但最终达到 ...
- react框架 Dva & Umi
概念 // http://localhost:3000/ //models import IndexPage from './routes/IndexPage'; import Products fr ...
- C#基础-for循环执行顺序
for(表达式1;表达式2;表达式3) {循环体} 执行顺序:1-表达式1赋值 2-判断表达式2是否为真 3-表达式2如果为否跳出for循环,如果为真执行循环体 4-执行表达式3 5-判断表达式2继续 ...
- 【element+vue后台页面】Vue-element-admin
https://segmentfault.com/a/1190000009275424
- Teamviewer 手机端怎么使用右键-已解决
Teamviewer 手机端怎么使用右键 可能很多人和我一样,关掉了那个提示,然后记不住操作 怎么控制电脑使用右键? 长按就可以了 拖到指定地点,然后长按不送,就会出来电脑右键菜单
- IEC_62304_CheckList
IEC 62304 Reference Software Lifecycle Process Applicable for Class A Class B Class C PRIMARY LIFECY ...
- 算法day01
- Centos 7配置docker-阿里云镜像加速
阿里云加速网址:https://cr.console.aliyun.com/cn-hangzhou/mirrors(自行注册账密码) sudo mkdir -p /etc/docker sudo vi ...