Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 51789 Accepted Submission(s): 17690
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
10
1
20 2
30 1
-1
Sample Output
20 10
40 40
40 40
题意:航电要分校区,要把设备尽可能的等分,给出不同设备的价值和数量。尽可能的等分,其实可以转化为一个最大价值是所有设备总价一半的背包里面最大可以放的物品价值总和,这样算出来的是较小的那一组,或者是和另一组价值相等,另一组只要所有设备总价值减去这一组就是了。最后的输入结束有点坑,测试数据里面不一定是-1.。。。。。。
#include<bits/stdc++.h>
using namespace std;
int dp[],v[];
int main() {
int n;
while(~scanf("%d",&n)&&n>)
{
memset(dp,,sizeof(dp));
memset(v,,sizeof(v));
int sum=,l=;
for(int i=;i<n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
while(b--)
{
v[l++]=a;//价值
sum+=a;
}
} for(int i=;i<l;i++)
{
for(int j=sum/;j>=v[i];j--)//因为要分成两组价值相近的,所以背包的总价值
{ //直接设成全部物品总价值的一半,因为这里时从一半的时候
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);//向下递减的,所以最后dp[sum/2]里面的
} //存的就是较小的那一组
}
printf("%d %d\n",sum-dp[sum/],dp[sum/]);
}
return ;
}