动态规划:HDU1059-Dividing(多重背包问题的二进制优化)
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8755 Accepted Submission(s): 2374
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.
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 a blank line after each test case.
#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");
}
}
#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(多重背包问题的二进制优化)的更多相关文章
- 51nod 多重背包问题(二进制优化)
有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...
- 51 Nod 1086 多重背包问题(二进制优化)
1086 背包问题 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放 ...
- 动态规划:HDU2844-Coins(多重背包的二进制优化)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- HDU 1059(多重背包加二进制优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDU-2844 Coins 多重背包 物品数量二进制优化
题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...
- luogu||P1776||宝物筛选||多重背包||dp||二进制优化
题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...
- CodeForces922E DP//多重背包的二进制优化
https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...
- 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了… ...
随机推荐
- JDBC让java程序连上数据库(mysql数据库)
一.小论异常: 其实JDK已经提供了一组API让java程序连上数据库,并执行SQL语句,其实说起来也蛮简单的,但是绝对是一个细致活,因为稍不留神,异常就铺天盖地的来了,下面说说这些异常吧(声明一下: ...
- Unity3d中使用assetbundle
1.导出assetbundle: ①单个资源导出成assetbundle: ②多个资源导出成一个assetbundle: 2.读取assetbundle: ①加载到内存: ②解压为具体资源. 1.导出 ...
- iOS开发ReactiveCocoa学习笔记(五)
ReactiveCocoa常见操作方法介绍: demo地址:https://github.com/SummerHH/ReactiveCocoa.git filter ignore ignoreValu ...
- 解决在eclipse中导入项目名称已存在的有关问题
新建项目-Import-File System-找到相应的文件夹-Overwrite existing resources without warning打钩,选中项目即可
- Android自定义控件练手——简单的时钟
首先这应该是一个老生常谈的设计了,但是毕竟身为小白的自己都没动手做过,不动手怎么提高自己呢,所以在这梅林沉船闲暇之际,我就把我的设计流程与思路记录下来.首先来看看效果图吧: 如上图就是一个简单并没有美 ...
- 使用CSS设置Chrome打印背景色
以下内容适用于Chrome浏览器 打印背景色可以通过在打印预览页面勾选背景图形实现 如果需要在用户不勾选的情况下依然能够打印背景色,可以通过css实现,如,table隔行设置背景色: .data-ta ...
- Oracle数据库基础--SQL查询经典例题
Oracle基础练习题,采用Oracle数据库自带的表,适合初学者,其中包括了一些简单的查询,已经具有Oracle自身特点的单行函数的应用 本文使用的实例表结构与表的数据如下: emp员工表结构如下: ...
- linux下如何实现mysql数据库定时自动备份
概述 备份是容灾的基础,是指为防止系统出现操作失误或系统故障导致数据丢失,而将全部或部分数据集合从应用主机的硬盘或阵列复制到其它的存储介质的过程.而对于一些网站.系统来说,数据库就是一切,所以做好 ...
- mybatis-分页和缓存
1.分页 1.1在dao接口中配置分页参数: package com.java1234.mappers; import java.util.List;import java.util.Map; imp ...
- 显示 Mac隐藏的文件夹 命令语句
默认情况下,模拟器的目录是隐藏的,要想显示出来,需要在Mac终端输入下面的命令 显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFil ...