天天看點

HDU 1171 Big Event in HDU 多重背包二進制優化

題目連結:

http://acm.hdu.edu.cn/showproblem.php?pid=1171

Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)

Memory Limit: 65536/32768 K (Java/Others)

#### 問題描述

> 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 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.

輸出

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.

樣例輸入

2

10 1

20 1

3

20 2

30 1

-1

樣例輸出

20 10

40 40

題意

有n類财産,每類的單價為a[i],數量為b[i],把這些财産分成總價值最接近的兩份sum1,sum2,且保證sum1>=sum2;

題解

多重背包,二進制優化成01背包

dp[i][j]表示前i個裡面是否能湊出價值為j的。

代碼

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf

typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;

const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);

//start----------------------------------------------------------------------

const int maxn=255555;

bool dp[maxn];
int n;

int main() {
    while(scf("%d",&n)==1&&n>0){
        clr(dp,0);
        vector<int> arr;
        dp[0]=true;
        int sum=0;
        for(int i=0;i<n;i++){
            int v,num;
            scanf("%d%d",&v,&num);
            sum+=v*num;
            for(int i=1;i<=num;i*=2){
                arr.pb(i*v);
                num-=i;
            }
            if(num){
                arr.pb(num*v);
            }
        }
//        rep(i,0,arr.sz()) prf("%d ",arr[i]); puts("");
        for(int i=0;i<arr.sz();i++){
            for(int j=maxn-1;j>=arr[i];j--){
                dp[j]|=dp[j-arr[i]];
            }
        }

        int ans_a=sum,ans_b=0;
        for(int i=1;i<=sum;i++){
            if(dp[i]==false) continue;
            int ta=i,tb=sum-i;
            if(abs(ans_a-ans_b)>abs(ta-tb)){
                ans_a=ta;
                ans_b=tb;
            }
        }

        if(ans_a<ans_b) swap(ans_a,ans_b);
        prf("%d %d\n",ans_a,ans_b);
    }
    return 0;
}

//end-----------------------------------------------------------------------