HDU1171--01背包
来源:互联网 发布:手机比价软件哪个好 编辑:程序博客网 时间:2024/05/17 21:40
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 36096 Accepted Submission(s): 12533
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
题目大意:
相当于一堆东西要分到两个篮子A和B中,让B中的价值量尽可能地靠近A但不能超过A。
解题思路:
无限靠近但不能超过,就是以总价值量的一半作为划分的界限。然后就拆解为01背包的问题了,背包大小是总背包的一半,往B的篮子中装,求能够装载的最大价值量。题目中给定的是物品的价值和对应的数量,转化为01背包,要变成一个个的单个物体,不能像题目中依靠价值的不同划分为不同的类别。如果划分为单个物品,极限考虑就是50种物品,物品的价值从1到50,每种物品有100个,得到的总背包大小就是(50+1)*50/2*100=1275000。同时,一般的01背包会有体积的说法,这里的话可以把物品的价值看作物品的体积,自己体会一下,比较容易懂。还有就是输入-1结束,不能写成
if(n == -1) return 0;因为这个WA很多次。改成if(n < 0) return 0;就A了。郁闷(-__-)b
源代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<stack>#include<queue>#include<vector>#include<deque>#include<map>#include<set>#include<algorithm>#include<string>#include<iomanip>#include<cstdlib>#include<cmath>#include<sstream>#include<ctime>using namespace std;int va[1275005];//(50+1)*50/2*100=1275000最大价值作为背包的最大空间int dp[1275005];int main(){ int n; int i,j; int v,num;//价值,数量 int count_num;//所有物品的数量 int count_pac;//总的背包大小 while(scanf("%d",&n) != EOF) { if(n < 0) { return 0; } memset(va,0,sizeof(va)); memset(dp,0,sizeof(dp)); j = 1; count_num = 0; count_pac = 0; for(i = 1; i <= n; i++) { scanf("%d%d",&v,&num); count_num += num; count_pac += num * v; while(num--) { va[j] = v; j++; } } //01背包 for(i = 1; i <= count_num; i++) { for(j = count_pac / 2; j >= va[i]; j--) { dp[j] = max(dp[j], dp[j - va[i]] + va[i]); } } printf("%d %d\n",count_pac - dp[count_pac / 2], dp[count_pac / 2]); } return 0;}
1 0
- HDU1171(01背包)
- hdu1171-01背包
- HDU1171--01背包
- hdu1171 01背包
- hdu1171 01背包
- hdu1171 01背包 | 多重背包
- hdu1171 转化01背包,组合
- hdu1171(01背包变形)
- hdu1171(01背包问题)
- HDU1171:Big Event in HDU(01背包)
- hdu1171 Big Event in HDU 01-背包
- hdu1171 Big Event in HDU 01背包
- HDU1171 一维01背包DP
- hdu1171 Big Event in HDU --01背包
- HDU1171:Big Event in HDU(01背包)
- HDU1171--Big Event in HDU--01背包,多重背包
- hdu1171多重背包
- hdu1171多重背包
- Python TCP 服务端
- 组合查询
- 【GDOI2017模拟8.12】字符串
- poj2513Network Saboteur(dfs+简单减枝)
- 操作符重载
- HDU1171--01背包
- 使用VIM支持鼠标操作
- 平衡二叉树
- CodeForces 589D
- Android之自定义实现BaseAdapter(普通实现)
- 150. Evaluate Reverse Polish Notation
- Android 6.0 运行时权限如何处理
- cocos编译安卓程序:Unsupported major.minor version 52.0
- 可调节灵敏度的ViewPager