HDU Bag Problem (并非0/1背包)

Bag Problem

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/131072 K (Java/Others)
Total Submission(s): 1011    Accepted Submission(s): 308


Problem Description
0/1 bag problem should sound familiar to everybody. Every earth man knows it well. Here is a mutant: given the capacity of a bag, that is to say, the number of goods the bag can carry (has nothing to do with the volume of the goods), and the weight it can carry. Given the weight of all goods, write a program that can output, under the limit in the above statements, the highest weight.
 

 

Input
Input will consist of multiple test cases The first line will contain two integers n (n<=40) and m, indicating the number of goods and the weight it can carry. Then follows a number k, indicating the number of goods, k <=40. Then k line follows, indicating the weight of each goods The parameters that haven’t been mentioned specifically fall into the range of 1 to 1000000000.
 

 

Output
For each test case, you should output a single number indicating the highest weight that can be put in the bag.
 

 

Sample Input
5 100 8 8 64 17 23 91 32 17 12 5 10 3 99 99 99
 

 

Sample Output
99 0
 

 

Source
 
 
用枚举做的,但是开了两个1<<20的数组来代替1<<41.之后就是一些必要的处理.
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int N=50;
const int INF=0x3f3f3f3f;

struct node{
    int weight;
    int num;
    void Init(){
        weight=0;
        num=0;
    }
}goodL[1<<20],goodR[1<<20];

int goods[N];
int start[N/2],end[N/2];

int cmp1(node a,node b){
    return a.weight==b.weight?a.num<b.num:a.weight<b.weight;
}

int cmp2(node a,node b){
    return a.num==b.num?a.weight<b.weight:a.num<b.num;
}

int Solve(int l,int r,int key){
    int mid,ans=0;
    while(l<=r){
        mid=(l+r)>>1;
        if(goodR[mid].weight==key)
            return key;
        if(goodR[mid].weight<key){
            ans=goodR[mid].weight;
            l=mid+1;
        }else
            r=mid-1;
    }
    return ans;
}

int main(){

    //freopen("input.txt","r",stdin);

    int n,m,k;
    while(~scanf("%d%d",&n,&m)){
        scanf("%d",&k);
        for(int i=0;i<k;i++)
            scanf("%d",&goods[i]);
        int l=k>>1,r=k-l;
        for(int i=0;i<(1<<l);i++){      //单独算左边的全组合
            goodL[i].Init();
            for(int j=0;j<l;j++)
                if((1<<j)&i){
                    goodL[i].num++;
                    goodL[i].weight+=goods[j];
                }
        }
        for(int i=0;i<(1<<r);i++){       //单独算右边的全组合
            goodR[i].Init();
            for(int j=0;j<r;j++)
                if((1<<j)&i){
                    goodR[i].num++;
                    goodR[i].weight+=goods[l+j];
                }
        }
        sort(goodL,goodL+(1<<l),cmp1);
        sort(goodR,goodR+(1<<r),cmp2);
        for(int i=0;i<=r;i++){  //得到个数为NUM时的起始地址和终止地址
            start[i]=INF;
            end[i]=-INF;
        }
        for(int i=0;i<(1<<r);i++){
            start[goodR[i].num]=min(start[goodR[i].num],i);
            end[goodR[i].num]=max(end[goodR[i].num],i);
        }
        int lim,tmp,pre=-1,ans=0;
        for(int i=0;i<(1<<l);i++){
            if(goodL[i].weight>m)
                break;
            if(pre==goodL[i].weight)
                continue;
            pre=goodL[i].weight;
            lim=n-goodL[i].num;
            for(int j=0;j<=min(r,lim);j++){
                tmp=Solve(start[j],end[j],m-goodL[i].weight);
                ans=max(ans,tmp+goodL[i].weight);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2013-05-19 09:04  Jack Ge  阅读(350)  评论(0编辑  收藏  举报