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.
       

题目大意就是判断这么多数字能不能均分成两类,每个数字不可拆分。

看完题目就感觉是个多重背包。不过这里只用判断sum/2能否被装到。于是就不用判断sum/2+1到sum的背包了。

由于只用判断是否能装到,于是只用开bool型数组即可。

由于每个数字有一定的使用次数,所以需要开vis数组,而且对于每一种数字的背包需要初始化全为1。

由于考虑到需要最优解,所以对于dp[j]为真的情况,就不需要再判断dp[j-i]了,因为如果再靠dp[j-i]来放下num[i]的话,就浪费了一次数字i的使用。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define N 1000000007 using namespace std; int num[], sum;
int vis[];
bool dp[]; bool Input()
{
sum = ;
memset(dp, , sizeof(dp));
for (int i = ; i <= ; ++i)
{
scanf("%d", &num[i]);
sum += i*num[i];
}
if (sum)
return true;
else
return false;
} void Work()
{
if (sum % )
{
printf("Can't be divided.\n\n");
return;
}
sum /= ;
dp[] = true;
for (int i = ; i <= ; ++i)
{
if (num[i] == )
continue;
memset(vis, , sizeof(vis));
for (int j = i; j <= sum; j++)
{
if (dp[j-i] && !dp[j] && vis[j-i] < num[i])
{
dp[j] = true;
vis[j] = vis[j-i]+;
}
}
}
if (dp[sum])
printf("Can be divided.\n\n");
else
printf("Can't be divided.\n\n");
} int main()
{
//freopen("test.in", "r", stdin);
int times = ;
while (Input())
{
printf("Collection #%d:\n", times);
Work();
times++;
}
return ;
}

ACM学习历程—HDU 1059 Dividing(dp && 多重背包)的更多相关文章

  1. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  2. hdu 1059 Dividing bitset 多重背包

    bitset做法 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a ...

  3. 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)

    Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...

  4. HDU 1059 Dividing(多重背包)

    点我看题目 题意: 将大理石的重量分为六个等级,每个等级所在的数字代表这个等级的大理石的数量,如果是0说明这个重量的大理石没有.将其按重量分成两份,看能否分成. 思路 :一开始以为是简单的01背包,结 ...

  5. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  6. HDU 1059 Dividing (dp)

    题目链接 Problem Description Marsha and Bill own a collection of marbles. They want to split the collect ...

  7. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  8. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  9. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

随机推荐

  1. 分区容量大于16TB的格式化

    File systems do have limits. Thats no surprise. ext3 had a limit at 16 TB file system size. If you n ...

  2. c++引用返回值

    引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...

  3. C++第4次实验(提高班)—继承和派生1

    从项目2和项目3中选1题作为实验.剩下2题写成作业. [项目1 - 龙三] 请在以下程序的横线处填上适当内容,以使程序完整,并使程序的输出为: Name: 龙三 Grade: 19 #include ...

  4. Windows下安装Oracle 11g全过程。

    1. 解压Oracle11.1.0.6 for win32,然后点击setup 2.选择高级安装,下一步 3.选择企业版,下一步 4.修改Oracle基目录,也可以是默认,下一步 5.将状态复选框打上 ...

  5. erlang中遍历取出某个位置的最大值

    例:有这么一个列表,A = [["abc","bds",3],["ssdss","dddx",2],["sfa ...

  6. ASP.NET MVC4+BootStrap 实战(一)

    好久没有写关于web开发的文章了,进到这个公司一直就是winform和Silverlight,实在是没有实战web项目的机会.大D也辞职了,去搞web app了.自己也该闲暇时间多学习学习,每天进步一 ...

  7. iOS_39_触摸解锁

    终于效果图: 控制器: // // BeyondViewController.m // 39_触摸解锁 // // Created by beyond on 14-9-17. // Copyright ...

  8. 宇视摄像机RTSP地址格式规则

    rtsp://{用户名}:{密码}@{ip}:{port}/video1/2/3,分别对应主/辅/三码流: 比如: rtsp://admin:admin@192.168.8.8:554/video1, ...

  9. WebService 简单应用

    Web服务全称:XML Web Service,是一种可编程的应用程序逻辑组件,它可以在Internet或企业网的Web应用程序之间共享.Web服务被设计成能够通过Internet与其它应用程序之间直 ...

  10. Notepad工具使用小技巧

    工欲善其事必先利其器 Notepad++是个很不错的文本编辑工具,掌握它的使用技巧可以提高我们工作的效率.见如下: 比较常用的罗列如下:(如果有更好的建议可以留言哈) 1: 添加书签 CTRL+F2 ...