HDU1171--Big Event in HDU(多重背包)
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 1139 Accepted Submission(s): 444 |
Problem Description
Nowadays, we all know that Computer College is the biggest
department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002. 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
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. A test case starting with a negative integer terminates input and this test case is not to be processed. |
Output
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. |
Sample Input
2 |
Sample Output
20 10 |
Author
lcy
|
这是一个多重背包的题目(将物品的原价值即为放入背包中物品的价值与花费,因为当不超过背包容量且价值之和最大时,即为不超过背包容量且花费之和最大(花费最接近背包容量),即为这些物品的原价值之和最接近背包容量。此处将物品总价值的一半看为背包容量即可。
这里有一个我见过的最诡异的八阿哥。。。汗。。。
这道题是以负数作为输入的结束标志的,而不只局限于-1, 被惯性思维坑了。。。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=;
const int inf=;
int v[MAXN];
int dp[MAXN];
int num[];
int main()
{
//freopen("data.in","r",stdin);
std::ios::sync_with_stdio(false);
std::cin.tie();
int n;
int sum;
int cc;
while(cin>>n&&n>=){
sum=;
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
cin>>v[i]>>num[i];
sum+=(v[i]*num[i]);
}
cc=sum/;
for(int i=;i<n;i++){
for(int k=;k<num[i];k++){
for(int j=cc;j>=v[i];j--){
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
}
}
}
cout<<sum-dp[cc]<<" "<<dp[cc]<<endl;
}
}
HDU1171--Big Event in HDU(多重背包)的更多相关文章
- hdu1171 Big Event in HDU(多重背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1171 多重背包题目不难,但是有些点不能漏或错. #include<iostream> #includ ...
- HDU 1171 Big Event in HDU 多重背包二进制优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...
- HDU - 1171 Big Event in HDU 多重背包
B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...
- HDU 1171 Big Event in HDU (多重背包变形)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1171:Big Event in HDU(多重背包分析)
通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...
- HDU-1171 Big Event in HDU(生成函数/背包dp)
题意 给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分. 思路 背包的思路显然是用一半总价值当作背包容量. 生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+.. ...
- 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 01-背包
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171 Problem ...
- HUD 1171 Big Event in HDU(01背包)
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
- HDU 1171 Big Event in HDU dp背包
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...
随机推荐
- 带密钥的sha1加密
带密钥的sha1加密: private static string HmacSha1Sign(string jsonStr, string secretKey, string enCoding ) { ...
- php 排序
for($i=1;$i<count($result);$i++) { $tmp = $result[$i]['PNL']; $tmp_ = $result[$i]; $j=$i-1 ; for( ...
- tableviewcell 中使用autolayout自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { [ce ...
- 获取android源码中遇到的问题
最近项目相当紧张,回家之后还需要继续研究android源码. 从网上找了一些资料,下载过程中还是遇到了很多问题.这里罗列一下: 1. 下载Repo过程中提示证书问题: curl https://dl- ...
- bkViewer V4.9k(数码照片浏览工具)中文免费版
软件名称: bkViewer 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 990KB 图片预览: 软件简介: 专为 ...
- js 中创建对象
对象是什么 从JavaScript定义上讲对象是无序属性的集合,其属性可以包含基本值.对象或函数.也就是说对象是一组没有特定顺序的属性,每个属性会映射到一个值上,是一组键值对,值可以是数据或对象. 最 ...
- APP测试--功能测试
1.1 了解需求 这一点,不但是功能测试,是所有测试都需要的第1步.通过需求文档,与产品经理的沟通,与开发的沟通,用户的使用习惯等各方法,了解APP的需求. 1.2 编写测试用例 当然之前可能是测试计 ...
- zabbix3.2在lamp环境安装
zabbix官网下载zabbix-3.2.1.tar.gz wget http://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20 ...
- Day06 杂乱与4个对象
1.杂乱 -- 数据库的分页操作 -- 分页使用的是rownum 例1: select rownum,empno,ename from emp ; 结果: 例2: select r,empno,ena ...
- CSS 各类 块级元素 行级元素 水平 垂直 居中问题
元素的居中问题是每个初学者碰到的第一个大问题,在此我总结了下各种块级 行级 水平 垂直 的居中方法,并尽量给出代码实例. 首先请先明白块级元素和行级元素的区别 行级元素 一块级元素 1 水平居中: ( ...