hdu2844 Coins 多重背包

來源:互聯網
上載者:User

標籤:class   blog   code   2014   string   os   



#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3f#define ll __int64using namespace std;int n,m,dp[100010],a[100010],c[1010];void pack01(int cost,int w){    for(int i=m;i>=cost;i--)        dp[i]=max(dp[i-cost]+w,dp[i]);}void packall(int cost,int w){    for(int i=cost;i<=m;i++)        dp[i]=max(dp[i-cost]+w,dp[i]);}void multipack(int cost,int w,int cnt){    if(cost*cnt>=m)//體積乘以數量大於總體積,說明不能完全裝完,相當於有無窮件,用完全背包        packall(cost,w);    else //可以裝完,用01背包    {        int i=1;        while(i<cnt)//二進位最佳化        {            pack01(cost*i,w*i);            cnt-=i;            i+=i;        }        pack01(cost*cnt,w*cnt);    }}int main(){    int i;    while(scanf("%d%d",&n,&m)&&(n||m))    {        for(i=0;i<n;i++)            scanf("%d",&a[i]);        for(i=0;i<n;i++)            scanf("%d",&c[i]);        memset(dp,0,sizeof dp);        for(i=0;i<n;i++)            multipack(a[i],a[i],c[i]);        int ans=0;        for(i=1;i<=m;i++)            if(dp[i]==i) ans++;        printf("%d\n",ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.