POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
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.
题意:给出6种大小的石头,问能不能分成数值一样的两堆。
思路:背包方面是简单的,但主要的问题是,如果一个一个枚举物品,物品太多肯定会超时。所以就有将物品的数量进行二进制分解,即分成2的幂次,每个2的幂都作为一个新的物品,再进行01背包就行了。POJ上交不但不支持<bits/stdc++.h>而且数组还要开大一点/.\
/** @Date : 2016-12-10-20.39
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include
#include
#include
#include
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; int cnt = 0;
int a[10];
int b[N]; int dp[60010]; void binaDiv(int x, int v)
{
int r = 1;
while(r < x)
{
b[cnt++] = r * v;
x -= r;
r <<= 1;
}
b[cnt++] = x * v;
} int main()
{
int c = 0;
while(true)
{
int sum = 0;
for(int i = 1; i <= 6; i++)
scanf("%d", a + i), sum += a[i]*i;
if(sum == 0)
break;
printf("Collection #%d:\n", ++c);
if(sum & 1)
{
printf("Can't be divided.\n\n");
continue;
}
sum /= 2; cnt = 0;
for(int i = 1; i <= 6; i++)
{
binaDiv(a[i], i);
}
MMF(dp);
for(int i = 0; i < cnt; i++)
{
for(int j = sum; j >= 0; j--)
{
if(j >= b[i])
dp[j] = max(dp[j], dp[j - b[i]] + b[i]);
}
}
if(dp[sum] == sum)
printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
return 0;
}
POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解的更多相关文章
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1059(多重背包加二进制优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others) Me ...
- hdu 2844 Coins (多重背包+二进制优化)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...
- hdu 2844 coins(多重背包 二进制拆分法)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)
HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...
- HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化)
HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化) 题意分析 首先C表示测试数据的组数,然后给出经费的金额和大米的种类.接着是每袋大米的 ...
- hdu1059 dp(多重背包二进制优化)
hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听 ...
随机推荐
- Alpha阶段中间产物——Thunder团队
Part One 版本控制 git地址:https://git.coding.net/lick468/iReader.git Part Two 软件功能说明书 相关链接:http://www.cnbl ...
- P4编程环境搭建遇到的问题与解决方法
在经历了无数的折腾之后,算是折腾,最后采用的是陈翔学长的脚本加上可爱的shell调整装好的. 链接:p4Install 也许是ubuntu18.04的问题,也有可能是我自己把这个系统折腾的有点杂乱的原 ...
- 3DMAX贴图无法显示
问题描述:我在点击"将材质指定给选定对象"按钮之后,模型只是变灰了,没有显示出我贴的图. 原因是:没有显示贴图. 我的解决方案:点击材质编辑器里面的"视口中显示敏感处理材 ...
- C# 执行bat文件
private void RunBat(string batPath) { Process pro = new Process(); FileInfo file = new FileInfo(batP ...
- ios::sync_with_stdio(false)提高C++读写速度
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:ios::sync_with_stdio(false)提高C++读写速度 本文地址:h ...
- Print之modile, level
一般print打印的design都会引入module, level. xxxprint(module, level, arg,...)... 每个Module都可以有各自的bitmap,代表这个mod ...
- 加密和数字签名工具GPG
转载: 源文件位置:http://blog.chinaunix.net/uid-9525959-id-2001824.html GPG [功能] GPG是加密和数字签名的免费工具,大多用于加密信息的 ...
- 解决编写 xml 没有代码提示
有时候在编写 struts.xml 会没有代码提示,一般是因为没有联网导致的,或者之前配置过 dtd 文件 url,但是文件路径之后被修改了. 解决方案有: 让电脑联网 修改 dtd 的本地路径以及 ...
- JavaScript 垃圾回收总结
JavaScript 是一门具有自动垃圾收集机制的编程语言,开发人员不必关心内存分配和回收的问题. 以下是对JavaScript的垃圾收集历程总结: 离开作用域的值将被自动标记为可以回收,因此将在垃圾 ...
- Java的第一个程序-Hello, World !
学了一个月的Java,现在总结一下,就算复习了. 一.安装Java环境 这个没啥好说的. 1. 官网下载JDK安装 2. 配置环境变量.注意的是:环境变量配置好以后,如果cmd中运行 java 命令没 ...