HDU1171-Big Event in HDU
描述:
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
代码:
多重背包转化为01背包问题,可以认为在一个容量为所有物品价值累加和的一半的背包中,尽量达到最大值。
第一版:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
#define N 105
#define M 1000
using namespace std; int main(){
int n,v[N],dp[N][M],a,b,volume,sum;
while( scanf("%d",&n)!=EOF && n> ){
volume=;sum=;
for( int i=;i<n;i++ ){
scanf("%d%d",&a,&b);
volume+=(a*b);
while( b-- )//将单个物品依次加入
v[sum++]=a;
}
sum--;
//需要初始化dp中的全部值为0,下一次dp比较的值,上一次可能没有赋值,比较时将与负值(未初始化)进行比较,得到不正确的值
//for( int i=0;i<volume;i++ )
// dp[0][i]=0;
memset(dp,,sizeof(dp)); for( int i=;i<=sum;i++ ){
for( int j=v[i];j<=volume/;j++ ){
dp[i][j]=max(dp[i-][j],dp[i-][j-v[i]]+v[i]);//转移方程
}
}
printf("%d %d\n",max(dp[sum][volume/],volume-dp[sum][volume/]),min(dp[sum][volume/],volume-dp[sum][volume/]));
}
system("pause");
return ;
}
测试用例:
2
10 1
20 1
3
10 1
20 2
30 1
-1
测试用例2答案是对的,用例1答案不对。因为对于用例1,20大于总价值一半(15),故dp值均为0,最后结果为0。为此,不妨使用优化版的dp,即使用一维数组dp,这样就算出现刚才的情况,较早的dp值即为答案。
值得注意的是,背包要求尽量装满,故初始化时需要全部赋值为0(不赋值则出错)。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
#define N 105
#define M 100000
using namespace std; int main(){
int n,v[N],dp[M],a,b,volume,sum;
while( scanf("%d",&n)!=EOF && n> ){
volume=;sum=;
for( int i=;i<n;i++ ){
scanf("%d%d",&a,&b);
volume+=(a*b);
while( b-- )//将单个物品依次加入
v[sum++]=a;
}
sum--;
//需要初始化dp中的全部值为0,下一次dp比较的值,上一次可能没有赋值,比较时将与负值(未初始化)进行比较,得到不正确的值
memset(dp,,sizeof(dp)); for( int i=;i<=sum;i++ ){
for( int j=volume/;j>=v[i];j-- ){
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);//转移方程
}
}
printf("%d %d\n",max(dp[volume/],volume-dp[volume/]),min(dp[volume/],volume-dp[volume/]));
}
system("pause");
return ;
}
HDU1171-Big Event in HDU的更多相关文章
- HDU-1171  Big Event in HDU
		
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
 - HDU1171——Big Event in HDU(母函数)
		
Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...
 - hdu1171 Big Event in HDU 01-背包
		
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171 Problem ...
 - hdu1171 Big Event in HDU(01背包)                                                                                            2016-05-28 16:32             75人阅读              评论(0)              收藏
		
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
 - hdu1171 Big Event in HDU(多重背包)
		
http://acm.hdu.edu.cn/showproblem.php?pid=1171 多重背包题目不难,但是有些点不能漏或错. #include<iostream> #includ ...
 - HDU-1171 Big Event in HDU(生成函数/背包dp)
		
题意 给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分. 思路 背包的思路显然是用一半总价值当作背包容量. 生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+.. ...
 - Big Event in HDU(HDU1171)可用背包和母函数求解
		
Big Event in HDU HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1 结果是:20 10.才最均匀! 三 ...
 - Big Event in HDU[HDU1171]
		
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
 - poj3211Washing Clothes(字符串处理+01背包)  hdu1171Big Event in HDU(01背包)
		
题目链接: id=3211">poj3211 hdu1171 这个题目比1711难处理的是字符串怎样处理,所以我们要想办法,自然而然就要想到用结构体存储.所以最后将全部的衣服分组,然 ...
 - Big Event in HDU
		
Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe ...
 
随机推荐
- STL中用erase()方法遍历删除元素
			
STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...
 - uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
			
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
 - [Leetcode][Python]31: Next Permutation
			
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
 - openstack core components use 总结
			
1,附加volume(块存储,云硬盘)到vmInstances(虚拟机实列)
 - python第三天---collections类
			
collection系列 1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. 我们从中挑选一些相对常用的方法来举例: 在上面的例子我们可以看出,counter方法返 ...
 - poj2728 Desert King --- 01分数规划 二分水果。。
			
这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...
 - Android图片裁剪之自由裁剪
			
我的博客http://blog.csdn.net/dawn_moon 客户的需求都是非常怪的.我有时候在给客户做项目的时候就想骂客户是sb.可是请你相信我,等你有需求,自己变成客户的时候,给你做项目的 ...
 - 浅谈Mybatis(二)
			
一.resultMap 作用:发现数据库的查询结果与实体之间不匹配时,需要通过ResultMap来进行映射处理.常用于多表查询. 多表查询还是比较复杂的,因为可能的情况很多.这里只说两种情况: 1.1 ...
 - [转]WIN7系统安装Apache 提示msvcr110.DLL
			
我的系统是WIN7 64位,安装配置Apache2.4.7(httpd-2.4.7-win64-VC11.zip )提示如下错误 VC++2012 2013 百度网盘地址:http://pan.bai ...
 - java_httpservice
			
http://blog.csdn.net/maosijunzi/article/details/41045181