Time Limit:
5000
MS
Memory Limit:
32768
KB
64bit IO Format:
%I64d & %I64u
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
思路:把价值看做容量,二分之后,就是多重背包模型,但是要注意,最开始 ans赋值不能随意,只有一件物品时候,有一个为0 的;
代码:
40 40
思路:把价值看做容量,二分之后,就是多重背包模型,但是要注意,最开始 ans赋值不能随意,只有一件物品时候,有一个为0 的;
代码:
// Create by 神舟 on 2015-02-12
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std ;
#define CLR (x ) memset (x , 0 , sizeof x )
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 1e4+5 ;
const int MOD = 2e5+5 ;
int k ,w [maxn ];
bool dp [MOD ];
int main ()
{
#ifdef LOCAL
freopen ( "in.txt" , "r" ,stdin );
//freopen("out.txt","w",stdout);
#endif
ios_base :: sync_with_stdio ( 0 );
int n ;
while ( scanf ( "%d" ,&n )!=EOF &&n > 0 ) {
k = 1 ;
CLR (w );
int v ,m ,sum = 0 ,ans = 0 ,t ;
for ( int i = 1 ;i <=n ;i ++) {
scanf ( "%d%d" ,&v ,&m );
for ( int c = 1 ;c <=m ;c <<= 1 ) {
w [k ++]=c *v ;
m -=c ;
sum +=c *v ;
}
if (m > 0 ) {
w [k ++]=m *v ;
sum +=m *v ;
}
}
k --;
memset (dp , false , sizeof dp );
dp [ 0 ]= true ;
for ( int i = 1 ;i <=k ;i ++) for ( int j =sum / 2 ;j >=w [i ];j --) {
if (dp [j -w [i ]]) {
dp [j ]= true ;
ans = max (ans ,j );
}
}
int res = max (ans ,sum -ans );
printf ( "%d %d \n " ,res ,sum -res );
}
return 0 ;
}