hdu1059(背包dp二进制优化)
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20691 Accepted Submission(s): 5827
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.
题解:
背包问题,二进制优化,对于价值为val有num个,可以将其分成1,2,4,8......这样的二进制价值,可以保证如果可以均分,是一定
可以的,如果可以均分,那么保证可以分出,如11 可以分出1,2,4,6,不可以均分,12分出1,2,4,5拿1,5或者2,4就均分了。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std; int Case=;
int cont=;
int a[],val[];
bool f[]; int main()
{
while(~scanf("%d%d%d%d%d%d",&a[],&a[],&a[],&a[],&a[],&a[])&&(a[]+a[]+a[]+a[]+a[]+a[]))
{
printf("Collection #%d:\n",++Case);
int sum=,mid=;
for (int i=;i<=;i++)
sum=sum+a[i]*i;
if (sum%)
{
printf("Can't be divided.\n\n");
continue;
}
mid=sum/;
memset(f,,sizeof(f));
f[]=;cont=;
for (int i=;i<=;i++)
{
int t=i;a[i]=a[i]*i;
while(a[i]>=t)
{
val[++cont]=t;
a[i]-=t;
t*=;
}
if (a[i]>) val[++cont]=a[i];
}
f[]=;
for (int i=;i<=cont;i++)
for (int j=mid;j>=val[i];j--)
if (f[j-val[i]]) f[j]=;
if (f[mid]) printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
}
hdu1059(背包dp二进制优化)的更多相关文章
- luogu||P1776||宝物筛选||多重背包||dp||二进制优化
题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...
- 动态规划: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 ...
- 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 ...
- HDU——2191悼念512汶川大地震遇难同胞(多重背包转化为01背包或二进制优化)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- HDU-2844 Coins 多重背包 物品数量二进制优化
题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...
- 多重背包的二进制优化——DP
#include<cstdio> #include<cstring> #include<algorithm> #define LL long long using ...
随机推荐
- [转]VC++的类头文件
本文转自:http://blog.csdn.net/forevertali/article/details/4370602 animal.h //在头文件中包含类的定义及类成员函数的声明 clas ...
- AJPFX浅谈关于Java程序员缺乏面向对象的基本功的问题
为什么很多 Java 程序员会缺乏面向对象基本功?这得怪那些 Java 框架.现在 Java 的各种框架太发达.太傻瓜化了,导致很多程序员只需要按部就班.照着框架进行代码填空,基本已经丧失了 OOA ...
- [BZOJ2705][SDOI2012]Longge的问题 数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 首先分析得题目所求$gcd(i,N)$的取值只可能是$N$的因子,则有$$Ans=\ ...
- 【学习笔记】深入理解js原型和闭包(7)——原型的灵活性
在Java和C#中,你可以简单的理解class是一个模子,对象就是被这个模子压出来的一批一批月饼(中秋节刚过完).压个啥样,就得是个啥样,不能随便动,动一动就坏了. 而在javascript中,就没有 ...
- qt5.8+vs2015使用Qt5WebEngine搭建环境
转载请注明出处:http://www.cnblogs.com/dachen408/p/7575094.html 1.项目属性,C/C++,所有选项,附加包含目录新增. $(QTDIR)\include ...
- C# 递归读取XML菜单数据
在博客园注册了有4年了,很遗憾至今仍未发表过博客,趁周末有空发表第一篇博客.小生不才,在此献丑了! 最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据 ...
- 嵌入式ARM开发板学习方法步骤
嵌入式开发就是指在嵌入式操作系统下进行开发,一般常用的系统有linux,android. 平台:Cortex-A9开发板 嵌入式技术学习如何入手,从何学起呢, 以下内容简单介绍嵌入式开发的学习步骤及如 ...
- ES6(vue)对象词法扩展
ES6 允许声明在对象字面量时使用简写语法,来初始化属性变量和函数的定义方法,并且允许在对象属性中进行计算操作: function getCar(make, model, value) { retur ...
- win手动编译JAVA 未完成(系统path未加入文章)
java 下面存.BAT dir /s /B *.java > sources.txtjavac @sources.txt -bootclasspath "C:\Users\88797 ...
- The following signatures couldn't be verified because the public key is not available: NO_PUBKEY XXXX
reference apt-key adv --recv-keys --keyserver keyserver.ubuntu.com XXXXXX apt-get update