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 
 

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 
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 
#include 
#include 
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<= 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;
}


 

你可能感兴趣的:(动态规划)