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 ...
随机推荐
- Spring Boot 注册 Servlet 的三种方法,真是太有用了!
本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spr ...
- ES-Apache Lucene
前言 在介绍Lucene之前,我们来了解相关的历史. 有必要了解的Apache Apache软件基金会(也就是Apache Software Foundation,简称为ASF)是专门为运作一个开源软 ...
- VBScript(一)
visual basic Script 好像是以个老掉牙的服务器端脚本语言,低版本的IE浏览器支持在浏览器里执行 几个特点 1. 大小写不敏感 2.在服务器端 inputBox, msgBox不被支持 ...
- VBA 连接sql server的用法
cnnstr = "Provider=sqloledb;Data Source=192.211.21.8;Initial Catalog=pub;UID=账号;PWD=密码" VB ...
- SQL——将两列合并成一列
将两列合并连接成一列,需要注意的是列的格式必须是NVARCHAR或者VARCHAR类型 ), call_uuid, ) +','+agent_code ' PerDate 1 ,980408102 ...
- 几个有关整数的证明(from信息安全数学基础的作业)
1. 设m,n为正整数,m为奇数,求证2m-1和2n+1互素 反证法:假设d=(2m-1,2n+1)≥2,则存在x,y∈z,2m=dx+1,和2n=dy-1 则存在u,v∈z,2mn=du+1,2nm ...
- 递推(三):POJ中的三道递推例题POJ 1664、POJ 2247和POJ 1338
[例9]放苹果(POJ 1664) Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. In ...
- k8s部署测试实例
查看运行中pod,并运行一个容器 [root@mast-1 k8s]# kubectl get pods No resources found. [root@mast-1 k8s]# kubectl ...
- 打开或关闭CD_ROM
实现效果: 知识运用: API函数 mciSendString //函数用来向媒体控制接口设备发送命令 声明如下 [DllImport("winmm.dll",EntryPoin ...
- vux安装
1. 在项目里安装vux cnpm install vux --save 2. 安装vux-loader cnpm install vux-loader --save-dev 3. 安装less-lo ...