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 ...
随机推荐
- c# webbrowser 获取用户选中文字
原文:c# webbrowser 获取用户选中文字 最近一直被一个问题困扰,有一个文本框,一个webbrowser控件,一个上下文菜单, webbrowser 获取用户选中文字" tit ...
- WinHEC(Windows硬件project产业创新峰会)将2015回归
WinHEC这是Windows Hardware Engineering Cumminity,中国呼吁Windows硬件project产业创新峰会.将2015在早期的回报,2015年3月18日至19日 ...
- hdu1588---Gauss Fibonacci(矩阵,线性复发)
根据题意:最后一步是寻求f(b) + f(k + b) + f(2 * k + b) + -+ f((n-1) * k + b) 清除f(b) = A^b 间A = 1 1 1 0 所以sum(n - ...
- sscanf()函数的用法
来自:http://blog.csdn.net/tigerjibo/article/details/6442151 sscanf 名称: sscanf() - 从一个字符串中读进与指定格式相符的数据. ...
- Same binary weight (位运算)
题目描述 The binary weight of a positive integer is the number of 1's in its binary representation.for ...
- HTML5 画一张图
笔者:本笃庆军 原文地址:http://blog.csdn.net/qingdujun/article/details/33344289 一.绘制图像 使用drawImage()方法绘制图像. 画图环 ...
- java_eclipse_设置全局编码_utf-8_编译class指定_运行jar乱码解决_不依赖环境
简述: javac时指定 编码 UTF-8 [ javac -encoding UTF-8 Test.java],运行时 java 指定编码 UTF-8 这样就不会出现乱码问题[ javac ...
- C#-简单的定时器(C# ConsoleApp) ---ShinePans
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpbmVwYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- 笔记28 mssql的update :from语法
原文:笔记28 mssql的update :from语法 笔记28 mssql的update :from语法 --mssql的update :from语法 --a表 b表 结构分别 id ,name ...
- 利用WebBrowser实现Web打印的分析
原文:利用WebBrowser实现Web打印的分析 WebBrowser是IE内置的浏览器控件,无需用户下载.本文档所讨论的是有关IE6.0版本的WebBrowser控件技术内容.其他版本的IE应该也 ...