HDU——1059Dividing(母函数或多重背包)
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23277 Accepted Submission(s): 6616
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.
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 a blank line after each test case.
1 0 0 0 1 1
0 0 0 0 0 0
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(母函数或多重背包)的更多相关文章
- HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)
HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- hdu 2844 Coins (多重背包)
题意是给你几个数,再给你这几个数的可以用的个数,然后随机找几个数来累加, 让我算可以累加得到的数的种数! 解题思路:先将背包初始化为-1,再用多重背包计算,最后检索,若bb[i]==i,则说明i这个数 ...
- hdu 1059 Dividing bitset 多重背包
bitset做法 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a ...
- HDU 2844 Coins(多重背包)
点我看题目 题意 :Whuacmers有n种硬币,分别是面值为A1,A2,.....,An,每一种面值的硬币的数量分别是C1,C2,......,Cn,Whuacmers想买钱包,但是想给人家刚好的钱 ...
- HDU 2844 Coins 【多重背包】(模板)
<题目连接> 题目大意: 一位同学想要买手表,他有n种硬币,每种硬币已知有num[i]个.已知手表的价钱最多m元,问她用这些钱能够凑出多少种价格来买手表. 解题分析: 很明显,这是一道多重 ...
- 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- hdu(1171)多重背包
hdu(1171) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 5445 Food Problem 多重背包
Food Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...
随机推荐
- socket使用非阻塞connect
在使用tcp的connect调用时,默认是使用阻塞方式,当服务器当前不可用时,connect会等待(内部在重试?)直到超时时间到达,而这个超时时间是系统内核规定的,不能使用setSocketOpt来设 ...
- C++拾遗(二)——初窥标准库类型
本篇博文的开始,先介绍一道书上看到的智力题:有20瓶药丸,其中19瓶装有1克/粒的药丸,余下一瓶装有1.1克/粒的药丸.有一台称重精准的天平,只是用一次天平的情况下如何找出比较重的那瓶药丸? 好了,直 ...
- sublime快捷键mark
Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)Ctrl+G 跳转到相应的行Ctrl+J 合并行(已选择需要合并的多行时)Ctrl+L 选择整行(按住-继续选择下 ...
- 特别困的学生 UVa12108(模拟题)
一.题目 课堂上有n个学生(n<=10).每个学生都有一个“睡眠-清醒”周期,其中第i个学生醒Ai分钟后睡Bi分钟,然后重复(1<=Ai,Bi<=5),初始第i个同学处于他的周期的C ...
- 移动端:active伪类无效的解决方法
:active伪类常用于设定点击状态下或其他被激活状态下一个链接的样式.最常用于锚点<a href="#">这种情况,一般主流浏览器下也支持其他元素,如button等. ...
- 新数据的GT列表
制作新数据集时需要重新制作train_GT,test_GT 代码: dic = {} with open('/home/bnrc/all_image_GT.txt','r') as file: for ...
- CPP-基础:运算符重载详解
1.运算符重载定义: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予已有运算符新的功 ...
- PHP的PDF扩展库TCPDF将中文字体设置为内嵌字体的方法
1. 下载要设置的字体,如名为simfang.ttf,放在./vendor/tecnickcom/tcpdf/tools目录中 2.在tools目录中按住shift,点击鼠标右键,点击“在此处打开命令 ...
- (4)JSTL的SQL标签库
jstl的SQL标签库 SQL tag Library中的标签用来提供在 JSP 页面中可以与数据库进行交互的功能Database access标签库有以下6组标签来进行工作: <sql:set ...
- commons-logging 和log4j包下载 Spring根据XML配置文件生成对象
需要用到Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/1qXLHzAW 以及日志j ...