Big Event in HDU
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
题目大意:有很多个人,其中每个人有一个自己的权值,权值一共有n种(这点我也没有太看懂,勉强理解),要分配成两个部分,要尽量保证每个部分的分数尽量接近,并且第一部分的权值要大于等于第二个部分。
刚刚看完揹包九讲,考虑使用01揹包来解决这个问题。首先计算总权值sum,以总权值的一半作为揹包的容量,由于第二部分的分数恒小于等于总权值一半,把第二部分当作揹包模型。又要保证两部分权值要尽量相等,所以把每个人的权值即当做放入揹包中的物体的体积,也当做物体的价值,就可以保证第二部分的权值小于第一部分却有尽可能接近第一部分的权值。
第一次的代码没有使用内存优化(想水过)导致了内存超限。
#include
#include
using namespace std;
struct node {
int a, b;
};
int s[5000][1250];
node a[52];
int main(int argc, const char * argv[]) {
int n;
while (cin >> n && n != -1) {
int sum = 0, ind, ave, ans1, ans2;
memset(s , 0 , sizeof(s));
for (int i = 1; i <= n; i++) {
cin >> a[i].a >> a[i].b;
sum += a[i].a * a[i].b;
}
ave = sum / 2;
ind = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= a[i].b; j++, ind++) {
for (int k = 0; k < a[i].a; k++)
s[ind][k] = s[ind - 1][k];
for (int k = a[i].a; k <= ave; k++)
s[ind][k] = s[ind - 1][k] > s[ind - 1][k - a[i].a] + a[i].a ? s[ind - 1][k] : s[ind - 1][k - a[i].a] + a[i].a;
}
/*
for (int i = 1; i <= ind - 1; i++) {
for (int j = 1; j <= ave; j++)
cout << s[i][j] << " ";
cout << endl;
}
*/
ans2 = s[ind - 1][ave];
ans1 = sum - ans2;
cout << ans1 << " " << ans2 << endl;
}
return 0;
}
第二次进行了内存优化,但是由于没有看见结束程序的条件是小于零,导致WA了很长一段时间。 附AC代码。
#include
#include
using namespace std;
struct node {
int a, b;
};
node s[51];
int val[250005];
int main(int argc, const char * argv[]) {
int n, sum, ave, ans1, ans2;
while (cin >> n && n > 0) {
sum = 0;
memset(val , 0 , sizeof(val));
for (int i = 1; i <= n; i++) {
cin >> s[i].a >> s[i].b;
sum += s[i].a * s[i].b;
}
ave = sum / 2;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= s[i].b; j++)
for (int k = ave; k >= s[i].a; k--)
val[k] = max(val[k] , (val[k - s[i].a] + s[i].a));
ans2 = val[ave];
ans1 = sum - ans2;
cout << ans1 << " " << ans2 << endl;
}
return 0;
}