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
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
#include <iostream>
using namespace std;
int c1[250010], c2[250010]; //c1保存结果,c2保存中间值
int value[55]; //物品的价值
int amount[55]; //物品的数量
int main()
{
int nNum;
while(scanf("%d", &nNum) && nNum>0)
{
memset(value, 0, sizeof(value));
memset(amount, 0, sizeof(amount));
int sum = 0;
for(int i = 1; i <= nNum; ++ i)
{
scanf("%d %d", &value[i], &amount[i]);
sum += value[i]*amount[i]; //计算物品的总价值
}
memset(c1, 0, sum*sizeof(c1[0]));
memset(c2, 0, sum*sizeof(c2[0]));
for(int i = 0; i <= value[1]*amount[1]; i += value[1])
c1[i] = 1;
int len = value[1]*amount[1]; //记录每次需循环的最大次数 **重点
for(int i = 2; i <= nNum; ++ i)
{
for(int j = 0; j <= len; ++ j)<span style="white-space:pre"> </span>//重点
for(int k = 0; k <= value[i]*amount[i]; k += value[i])
{
c2[k + j] += c1[j]; //在原来的基础上加
}
len += value[i]*amount[i]; //每次循环后,最大次数增加<span style="white-space:pre"> </span>**重点
for(int j = 0; j <= len; ++ j)
{
c1[j] = c2[j]; //将c2中的值转移到c1中去
c2[j] = 0; //c1全赋值为0
}
}
for(int i = sum/2; i >= 0; -- i) //从中间开始向前搜索
if(c1[i] != 0)
{
printf("%d %d\n", sum - i, i);
break;
}
}
return 0;
}