背包模版 自己用(hdu2844)

来源:互联网 发布:电子狗数据升级包 编辑:程序博客网 时间:2024/05/15 06:14
#include<stdio.h>#include<string.h>struct node{    int value,number;}a[110];#define maxx 100010int f[maxx];/*procedure MultiplePack(cost,weight,amount)    if cost*amount>=V        CompletePack(cost,weight)        return    integer k=1    while k<amount        ZeroOnePack(k*cost,k*weight)        amount=amount-k        k=k*2    ZeroOnePack(amount*cost,amount*weight)*/int max(int x,int y){ if(x>y) return x; return y;    }void Complete(int cost,int weight,int m){    for(int i=cost;i<=m;i++)        f[i]=max(f[i],f[i-weight]+cost);  }void Zero_One(int cost,int weight,int m){        for(int i=m;i>=cost;i--)          f[i]=max(f[i],f[i-weight]+cost);}void MultiplePack(int cost,int weight,int m,int number){       // int t=a[i].value*a[i].number;        if(weight*number>m)            Complete(cost,weight,m);        else            {                int k=1;                while(k<number)                {                    Zero_One(k*cost,k*cost,m);                    number-=k;                    k<<=1;                    }                Zero_One(number*cost,number*cost,m);             }}int main(){    int n,m,count,i,j,k,t;    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n==0 && m==0)break;                for(i=0;i<n;i++)            scanf("%d",&a[i].value);        for(i=0;i<n;i++)            scanf("%d",&a[i].number);        memset(f,0,sizeof(f));                for(i=0;i<n;i++)        {            MultiplePack(a[i].value,a[i].value,m,a[i].number);         }          int ans=0;          for(int i=1;i<=m;i++)          {                  if(f[i]==i)                  ans++;          }                printf("%d\n",ans);   }              }

原创粉丝点击