Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21625 Accepted Submission(s): 7581
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
2 10 1 20 1 3 10 1 20 2 30 1 -1
Sample Output
20 10 40 40
Author
lcy
明显是多重背包问题,输入数据的时候把数据总和累加出来
再取背包容量为总和的一半,找出大于等于背包容量并最接近背包容量的方案
由于多重背包的状态方程是:dp[i][v] = max{ dp[i-1][v-k*n[i]]+k*w[i] }---
题中n[i]、w[i]相等
使用该方法会超时,因此化作01背包处理
把所有物品的w[i]均加入数组a中,如果有n个,则加入n个w[i]到a数组中
01背包的状态方程是:dp[i][v] = max{ dp[i-1][v], dp[i-1][v-n[i]]+w[i] }
其中dp[i][v]的含义是用容量为v的背包装前i个物品的最优方案,所以当前第i个物品可以选择装或不装,对应dp[i-1][v], dp[i-1][v-n[i]]+w[i]
但是用二维数组会超内存,因此需要在01背包的基础上用滚动数组优化
因为计算dp[i][v]时,只需考虑dp[i-1][v]和dp[i-1][v-n[i]]+w[i],每个dp[i][v]仅对应上面一行该列和上面一行前一列。因此可以把dp化为一维数组来保存上次的状态
代码如下:
#include <map>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define esp 1e-9
#define MAXN 100010
#define ll long long
#define INF 0x7FFFFFFF
#define BUG system("pause")
#define SW(a,b) a^=b;b^=a;a^=b;
using namespace std;
int a[MAXN];
int dp[MAXN];
int main(void){
int T;
while(cin >> T, T>0){
int sum = 0;
int w, n;
int index = 0;
for(int i=0; i<T; ++i){
cin >> w >> n;
sum += w*n;
while(n--){
a[index++] = w;
}
}
int v = sum%2==0 ? sum/2 : sum/2+1;
memset(dp, 0, sizeof(dp));
for(int i=0; i<index; ++i){
for(int j=v; j>=0; --j){
if(j-a[i] >= 0)
dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
// cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << "\t";
}
// cout << endl;
}
// cout << "\n\n\n";
if(dp[sum/2] <= sum/2)
dp[sum/2] = sum - dp[sum/2];
cout << dp[sum/2] << " " << sum-dp[sum/2] << endl;
}
return 0;
}