Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23277    Accepted Submission(s): 6616

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.

看看能否凑出V/2这个值就可以了。母函数比较好理解

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=1300;
int c[7];
int c1[N],c2[N];
int main(void)
{
int i,j,k,V,tcase=0;
while (~scanf("%d%d%d%d%d%d",&c[1],&c[2],&c[3],&c[4],&c[5],&c[6]))
{
V=0;
for (i=1; i<=6; i++)
{
c[i]%=60;
V+=c[i]*i;
}
if(!V)
break;
if(V&1)
{
printf("Collection #%d:\nCan't be divided.\n\n",++tcase);
continue;
}
V>>=1;
MM(c1);MM(c2);
int index=0;
for (i=1; i<=6; i++)
{
if(c[i])
{
for (j=0; j<=c[i]; j++)
{
c1[j*i]=1;
index=i;
}
break;
}
}
for (i=index+1; i<=6; i++)
{
for (j=0; j<=V; j++)
{
if(c1[j])
{
for (k=0; k<=c[i]; k++)
{
if(j+i*k>V)
break;
c2[j+i*k]+=c1[j];
}
}
}
memcpy(c1,c2,sizeof(c2));
MM(c2);
}
printf("Collection #%d:\n",++tcase);
puts(c1[V]?"Can be divided.\n":"Can't be divided.\n");
MM(c);
}
return 0;
}

然后是多重背包的。感觉以后这样的题还是写成自定义函数吧。不然课件上非常难看懂。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=122000;
int dp[N];
int V;
int c[7];
void zonepack(int cost,int val)
{
for (int i=V; i>=cost ; --i)
{
dp[i]=max(dp[i],dp[i-cost]+val);
}
}
void wanquanpack(int cost,int val)
{
for (int i=cost; i<=V ; ++i)
{
dp[i]=max(dp[i],dp[i-cost]+val);
}
}
int main(void)
{
int n,i,j,k,tcase=0,cnt;
while (~scanf("%d%d%d%d%d%d",&c[1],&c[2],&c[3],&c[4],&c[5],&c[6])&&(c[1]||c[2]||c[3]||c[4]||c[5]||c[6]))
{
cnt=0;
V=0;
for (i=1; i<=6; i++)
V+=c[i]*i;
if(V&1)
{
printf("Collection #%d:\n%s\n\n",++tcase,"Can't be divided.");
continue;
}
V>>=1;
MM(dp);
for (i=1; i<=6; i++)
{
if(c[i]*i>=V)
{
wanquanpack(i,i);
}
else
{
int k=1,t=c[i];
while (k<t)
{
zonepack(k*i,k*i);
t-=k;
k<<=1;
}
zonepack(t*i,t*i);
}
}
printf("Collection #%d:\n%s\n\n",++tcase,dp[V]!=V?"Can't be divided.":"Can be divided.");
}
return 0;
}

HDU——1059Dividing(母函数或多重背包)的更多相关文章

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

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

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

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

  3. hdu 2844 Coins (多重背包)

    题意是给你几个数,再给你这几个数的可以用的个数,然后随机找几个数来累加, 让我算可以累加得到的数的种数! 解题思路:先将背包初始化为-1,再用多重背包计算,最后检索,若bb[i]==i,则说明i这个数 ...

  4. hdu 1059 Dividing bitset 多重背包

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

  5. HDU 2844 Coins(多重背包)

    点我看题目 题意 :Whuacmers有n种硬币,分别是面值为A1,A2,.....,An,每一种面值的硬币的数量分别是C1,C2,......,Cn,Whuacmers想买钱包,但是想给人家刚好的钱 ...

  6. HDU 2844 Coins 【多重背包】(模板)

    <题目连接> 题目大意: 一位同学想要买手表,他有n种硬币,每种硬币已知有num[i]个.已知手表的价钱最多m元,问她用这些钱能够凑出多少种价格来买手表. 解题分析: 很明显,这是一道多重 ...

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

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

  8. hdu(1171)多重背包

    hdu(1171) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. hdu 5445 Food Problem 多重背包

    Food Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...

随机推荐

  1. C 函数库 (libc,glibc,uClibc,newlib)

    glibc glibc和libc都是Linux下的C函数库,libc是Linux下的ANSI C的函数库:glibc是Linux下的GUN C的函数库:GNU C是一种ANSI C的扩展实现.ANSI ...

  2. 解决Unsupported major.minor version 51.0报错问题

    问题产生原因:计算机环境变量的jdk版本与eclipse使用的jdk版本不一致 解决方法: 1.查看计算机环境变量的jdk版本 2.查看eclipse项目java compiler的方法:在项目点右键 ...

  3. Oracle错误(包括PL/SQL)集合与修复

    +-----------------------------------------------------------------------+ |   在本篇随笔中,仅根据个人经验累积错误进行描述 ...

  4. 数据库-SQL语法:LEFT JOIN

    LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行.(补充:left join是一对多的关系,表里所有符合条件的记 ...

  5. Web开发面临的挑战主要有哪些?

    摘要:要成为一名高效的Web开发者,这需要我们做很多工作,来提高我们的工作方式,以及改善我们的劳动成果.而在开发中难免会遇到一些困难,从前端到后端. 导读:要成为一名高效的Web开发者,这需要我们做很 ...

  6. React初识整理(二)--生命周期的方法

    React生命周期主要有7中: 1. componentWillMount() :组件将要挂载时触发 ,只调用1次 2. componentDidMount() :组件挂载完成时触发,只调用1次 3. ...

  7. 【图论】hdu6370Werewolf

    有理有据的结论题 Problem Description "The Werewolves" is a popular card game among young people.In ...

  8. 文件操作-cp

    Linux cp命令 也是我们在实际使用中非常常用的一个命令,主要用来复制文件.文件夹等.今天就来给大家介绍下 cp命令 的使用. 转载自 https://www.linuxdaxue.com/lin ...

  9. Linux-MySQL基本命令-SQL语句

    服务端命令SQL 在数据库系统中,SQL语句不区分大小写(建议用大写) SQL语句可单行或多行书写,以“;”结尾 关键词不能跨多行或简写 用空格和缩进来提高语句的可读性 子句通常位于独立行,便 ...

  10. 面向对象之多态,property

    多态: 同一种事物有多种状态 多态性: 在不考虑对象具体类型的前提下直接调用对象下的方法 静态多态性和动态多态性 静态多态性:都可以进行+操作 动态多态性:不考虑对象具体类型调用方法 多态的好处: ① ...