hdu 1059 Dividing 多重背包
点击打开链接链接
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17544 Accepted Submission(s): 4912
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 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Collection #1:
Can't be divided. Collection #2:
Can be divided.
给出价值分别为1 2 3 4 5 6 的石头的个数 问能不能平分
一个容量为sum/2的背包
#include<cstdio>
#include<cstring>
int maxn(int a,int b)
{
return a>b?a:b;
}
int cnt[10];
int w[22222];
int dp[22222];
int main()
{
int sum;
int cas=1;
int xx;
while(scanf("%d",&cnt[0])!=EOF)
{
sum=cnt[0];
for(int i=1;i<6;i++)
{
scanf("%d",&cnt[i]);
sum+=cnt[i]*(i+1);
}
if(!sum)
break;
xx=0;
for(int i=0;i<6;i++)
{
for(int j=1;j<=cnt[i];j*=2)
{
w[xx++]=j*(i+1);
cnt[i]-=j;
}
if(cnt[i])
{
w[xx++]=cnt[i]*(i+1);
cnt[i]=0;
}
}
printf("Collection #%d:\n",cas++);
//printf("%d\n",sum);
if(sum%2)
{
printf("Can't be divided.\n");
}
else
{
memset(dp,0,sizeof(dp));
for(int i=0;i<xx;i++)
{
for(int j=sum/2;j>=w[i];j--)
{
dp[j]=maxn(dp[j],dp[j-w[i]]+w[i]);
}
}
//printf("%d\n",dp[sum/2]);
if(dp[sum/2]==sum/2)
{
printf("Can be divided.\n");
}
else
{
printf("Can't be divided.\n");
}
}
puts("");
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
hdu 1059 Dividing 多重背包的更多相关文章
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- HDU 1059(多重背包加二进制优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- hdu 1059 Dividing bitset 多重背包
bitset做法 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a ...
- HDU 1059 Dividing 分配(多重背包,母函数)
题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为1.2.3.4.5.6共6个价钱.问:是否能公平分配? 输入: 每行为一个测试例子,每行包括6个数字,分别对 ...
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- ACM学习历程—HDU 1059 Dividing(dp && 多重背包)
Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...
- 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
随机推荐
- IE浏览器兼容性问题解决方法
如何用一行代码来解决CSS各种IE各种兼容性问题 一行代码来解决CSS在,IE6,IE7,IE8,IE9,IE10 各种兼容性问题. 在站点前端写代码的过程中,非常多时间IE各个版本号的兼容问题非常难 ...
- mysql函数二
四.条件推断函数 1.if(expr,v1,v2)函数:成立返回结果v1,否则结果v2 例:select id,if(grade>=60,'pass','fail') from t; 2.IFN ...
- 半平面交总结and模板
博客原文地址:http://blog.csdn.net/xuechelingxiao/article/details/40859973 这两天刷了POJ上几道半平面交,对半平面交有了初步的体会,感觉半 ...
- Delphi 3D Glscene安装
GLScene开源库Delphi基于提供OpenGL的3D框架.由GLScene组件,您可以轻松地创建和渲染你的3D幕后.令人奇怪的是,.对于这样一个很好的开源库.该网络无法找到完整的安装说明,甚至G ...
- Spring3.0学习笔记文档的官方网站(六)--3.4.1
3.4 依靠 3.4.1 依赖注入 依赖注入两种方式:基于构造函数DI.基于setter方法DI. 3.4.1.1 基于构造函数DI 参数是引进一个对象的.和缺乏父母之前-子类关系: ...
- 开发并调试 Mail Add-in
开发并调试 Mail Add-in (mail app for Outlook) 准备工作 如果你的邮箱搭建在 Exchange Server 上,则可以创建邮件应用程序(Mail Add-in)来扩 ...
- Memcache功能具体解释
memcache函数全部的方法列表例如以下: Memcache::add – 加入一个值.假设已经存在,则返回false Memcache::addServer – 加入一个可供使用的server地址 ...
- KMP算法之从next[]到nextVal[] (转)
前些日子写了一篇KMP算法的博文,浅谈数据结构之KMP(串中的模式匹配算法),在这片文章中,谈到了一个模式串K值的记录数组 next[],详细可看那篇文章,其实,前面定义的next[]数组是有一定缺陷 ...
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
原文:如何解决FormView中实现DropDownList连动选择时出现 "Eval().XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的 ...
- java_linux_shell_定时kill 启动java程序
#!/bin/bash #while truedo Process_ID=`ps -ef |grep 'LoginSinaWeiboCookie.jar' |grep -v grep |awk '{p ...