Hdu1171 -- c-big event in HDU (multiple backpacks + binary optimization)

Source: Internet
Author: User
C-big event in HDU Time limit:5000 Ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit 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 shoshould be as equal as possible. at the same time, You shoshould 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 (multiple backpacks + binary optimization)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.