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 ...
随机推荐
- uilabel 复制
//添加一个长按响应方法 - (void)addLongPressGestureRecognizer { UILongPressGestureRecognizer * longPress = [[UI ...
- 重识 CSS
- Android获取网络数据进行GZIP解压
说明:现在很多网站都会在回传数据的时候进行GZIP压缩,我们可以在请求头中申明支持GZIP压缩.可以减轻网络传输压力,Xutils中已经实现. 下面是一个DEMO,便于理解. private void ...
- Elasticsearch Java虚拟机配置详解(转)
引言: 今天,事情终于发生了.Java6(Mustang),是2006年早些时候出来的,至今仍然应用在众多生产环境中,现在终于走到了尽头.已经没有什么理由阻止迁移到Java7(Dolphin)上了. ...
- Android 内存监测工具 DDMS --> Heap(转)
DDMS 的全称是Dalvik Debug Monitor Service,它为我们提供例如:为测试设备截屏,针对特定的进程查看正在运行的线程以及堆信息.Logcat.广播状态信息.模拟电话呼叫.接收 ...
- HDU 5769 Substring
后缀数组. 然后按照排序完成之后的顺序,每个后缀统计贡献量. 统计第i个后缀的贡献的时候,如果这个后缀中没有X,贡献度为0. 有贡献的分3种情况考虑: 1.如果这个后缀height部分等于0(即与前一 ...
- Linux Shell : Test命令参数解析
格式: test conditions test -n string : string 不为空 test -z string : string 为空 test int1 -eq int2 : int ...
- Entity Framework教程
随着Code First一起出现的DbContext和DbSet类绝对可以称得上EF的功能核心,其取代了之前的ObjectContext和ObjectSet类,提供了与数据库通信,管理内存中实体的重要 ...
- MC 自己平均
using System; using System.Drawing; using System.Linq; using System.Collections; namespace PowerLang ...
- 脚本添加crontab任务【转】
今天朋友问,计划任务是用crontab -e来添加的,如何使用脚本来添加呢? 在执行crontab -e命令时,会在/var/spool/cron目录下创建一个文件,文件的名称是你当前的用户名,内容就 ...