首页 > 代码库 > hdu1171--C - Big Event in HDU(多重背包+二进制优化)

hdu1171--C - Big Event in HDU(多重背包+二进制优化)

C - Big Event in HDU
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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

210 120 1310 1 20 230 1-1
 

Sample Output

20 1040 40
 

 

 

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{    int v , m ;} q[100];int dp[260000] ;int main(){    int n , ans , i , j , k , sum , s , l;    while(scanf("%d", &n) && n > 0)    {        ans = 0 ;        for(i = 0 ; i < n ; i++)        {            scanf("%d %d", &q[i].v, &q[i].m);            ans += q[i].v*q[i].m ;        }        sum = ans ;        ans = ans / 2 ;        memset(dp,0,sizeof(dp));        dp[0] = 1 ;        for(i = 0 ; i < n ; i++)        {            k = -1 ;            while( (1<<(k+1)) -1 < q[i].m )                k++ ;            for(j = 0 ; j <= k ; j++)            {                if(j == k) s = q[i].m - (1<<j) + 1 ;                else s = 1<<j ;                for(l = ans ; l >= s*q[i].v ; l--)                    if( !dp[l] && dp[ l-s*q[i].v ] )                        dp[l] = 1 ;            }        }        for(i = ans ; i >= 0 ; i--)        {            if( dp[i] )            {                printf("%d %d\n", sum-i, i);                break;            }        }    }    return 0;}


 

hdu1171--C - Big Event in HDU(多重背包+二进制优化)