【杭电oj】1171 - Big Event in HDU(01背包)
来源:互联网 发布:mysql增删改查 编辑:程序博客网 时间:2024/06/05 19:12
点击打开题目
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35139 Accepted Submission(s): 12183
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).
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.
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
210 120 1310 1 20 230 1-1
Sample Output
20 1040 40
Author
lcy
用01背包,把每一个物品和价值都拆开放入数组。
注意:题目说是 n 为负数时结束,不是 -1。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dp[250011];int v[500011];int main(){int n;int mid;int sum;int ant;while (~scanf ("%d",&n) && n >= 0){ant = 0;sum = 0;for (int i = 1 ; i <= n ; i++){int t1,t2;scanf ("%d %d",&t1,&t2);sum += t1 * t2;while (t2--)v[ant++] = t1;}mid = sum >> 1;memset (dp,0,sizeof (dp));for (int i = 0 ; i < ant ; i++){for (int j = mid ; j >= v[i] ; j--)dp[j] = max (dp[j] , dp[j-v[i]] + v[i]);}printf ("%d %d\n",sum - dp[mid],dp[mid]);}return 0;}
0 0
- 【杭电oj】1171 - Big Event in HDU(01背包)
- 杭电1171Big Event in HDU(01背包)
- 杭电-1171 Big Event in HDU(完全背包转换01背包)
- 杭电1171 Big Event in HDU(母函数+多重背包解法)
- 杭电acm 1171Big Event in HDU(0-1背包)
- 杭电 1171 Big Event in HDU
- 杭电1171-Big Event in HDU
- 杭电1171 Big Event in HDU
- 杭电1171Big Event in HDU
- HDU 1171 Big Event in HDU(01背包)
- hdu 1171 Big Event in HDU(01背包)
- HDU 1171 Big Event in HDU(01背包问题)
- HDU 1171 Big Event in HDU(01背包)
- hdu 1171 Big Event in HDU (01背包)
- HDU 1171 Big Event in HDU(01背包)
- HDU 1171 Big Event in HDU(01背包)
- hdu 1171 Big Event in HDU(01背包变形)
- HDU 1171 Big Event in HDU (DP--01背包)
- iOS 匹配任意关键字
- Linux Wireless Networking: a short walk
- 浅谈C语言中结构体的初始化
- hdu5742 It's All In The Mind(水)
- 南阳acm 57-6174问题
- 【杭电oj】1171 - Big Event in HDU(01背包)
- 【数据库】解释关系代数中的象集、除运算
- B - Can you solve this equation?
- 在VM中安装linux Red Hat7.0
- Notepad++ 快捷键整理
- Notification通知
- Yii2.0自带验证码使用心得分享 [ 2.0 版本 ]
- 贪心-HDU-5742-It's All In The Mind
- OC_模糊搜索(精华版)