Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28020    Accepted Submission(s): 9864

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
10 1
20 1
3
10 1
20 2
30 1
-1
 
Sample Output
20 10
40 40
 
Author
lcy
 
题目意思:
给n不同种类的物品,每种物品有自己的价值w[i]和个数num[i],现把全部物品分为两部分,使得两部分总价值最接近,输出两部分总价值。
 
 
思路:
物品总价值为sum,那么每部分的总价值最接近sum/2。设一个体积为sum/2的背包,那么问题就转化为选择一些物品使得sum/2的背包中装最大价值的物品,01背包模型。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 55*50*100/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int v[], num[];
int dp[N]; main()
{
int i, j, k;
while(scanf("%d",&n)==&&n>=){
int sum=;
for(i=;i<=n;i++){
scanf("%d %d",&v[i],&num[i]);
sum+=v[i]*num[i];
}
memset(dp,,sizeof(dp));
for(i=;i<=n;i++){
for(k=;k<=num[i];k++){
for(j=sum/;j>=k*v[i];j--){
dp[j]=max(dp[j],dp[j-v[i]*k]+v[i]*k);
}
}
}
printf("%d %d\n",sum-dp[sum/],dp[sum/]);
}
}

最新文章

  1. 关于MySql的1146错误修正
  2. Linux多线程学习总结
  3. sql中update,alter,modify,delete,drop的区别和使用(整理)(转)
  4. C++多线程编程(三)线程间通信
  5. HDU 2722 Here We Go(relians) Again
  6. asp.net下利用MVC模式实现Extjs表格增删改查
  7. 基于visual Studio2013解决面试题之0609寻找链表公共节点
  8. docker k8s 1.3.8 + flannel
  9. React-intl 实现多语言
  10. 1.Java 加解密技术系列之 BASE64
  11. 深入浅出数据结构C语言版(17)——希尔排序
  12. mssql执行计划查看的一些知识
  13. JAVA基础第四章-集合框架Collection篇
  14. JS学习笔记Day18
  15. Oracle 中 编写 function 和 procedure 的注意事项
  16. Mybatis-利用resultMap 输出复杂pojo
  17. 状压dp-----三进制
  18. centos下搭建sockets5代理
  19. API gateway 之 kong 安装
  20. &lt;---------------------装箱,拆箱的过程--------------------------&gt;

热门文章

  1. asp获取文件名和扩展名的函数代码
  2. hibernate中load和get方法的区别
  3. wex5 教程 之 图文讲解 全局可观察变量与登陆状态全局控制
  4. CSS 笔记五(Combinators/Pseudo-classes/Pseudo-elements)
  5. MySQL如何关联查询
  6. dom4j-1.6.1.jar与dom4j-1.4.jar
  7. [LeetCode_1] twoSum
  8. illustrator将图片转换成ai路径
  9. 各种浏览器(IE,Firefox,Chrome,Opera)COOKIE修改方法[转]
  10. javascript实现动态侧边栏