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
1
20 2
30 1
-1
Sample Output
20 10
40 40
题意:航电要分校区,要把设备尽可能的等分,给出不同设备的价值和数量。尽可能的等分,其实可以转化为一个最大价值是所有设备总价一半的背包里面最大可以放的物品价值总和,这样算出来的是较小的那一组,或者是和另一组价值相等,另一组只要所有设备总价值减去这一组就是了。最后的输入结束有点坑,测试数据里面不一定是-1.。。。。。。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int dp[500005],v[5005]; 4 int main() { 5 int n; 6 while(~scanf("%d",&n)&&n>0) 7 { 8 memset(dp,0,sizeof(dp)); 9 memset(v,0,sizeof(v)); 10 int sum=0,l=0; 11 for(int i=0;i<n;i++) 12 { 13 int a,b; 14 scanf("%d %d",&a,&b); 15 while(b--) 16 { 17 v[l++]=a;//价值 18 sum+=a; 19 } 20 } 21 22 for(int i=0;i<l;i++) 23 { 24 for(int j=sum/2;j>=v[i];j--)//因为要分成两组价值相近的,所以背包的总价值 25 { //直接设成全部物品总价值的一半,因为这里时从一半的时候 26 dp[j]=max(dp[j],dp[j-v[i]]+v[i]);//向下递减的,所以最后dp[sum/2]里面的 27 } //存的就是较小的那一组 28 } 29 printf("%d %d ",sum-dp[sum/2],dp[sum/2]); 30 } 31 return 0; 32 }