HDU 2844 Coins Multiple backpack

Source: Internet
Author: User

Poke here: 2844

Review the knapsack problem

Test instructions: Tell you the value and quantity of the coins in N, how many different denominations can be composed, the denomination is limited in the interval [1, M]

Idea: Cover one side with a backpack, take the space of the coin for its own value, then when the dp[i] = = i, the space for I backpack back filled, that can constitute the case of denomination I

1#include"bits/stdc++.h"2 using namespacestd;3 intN, M;4 intval[ the], num[ the];5 intdp[100010];6 7 voidZopack (intSpaintval)8 {9     inti;Ten      for(i = M; I >= spa;--i) { OneDp[i] = max (Dp[i-spa] +Val, dp[i]); A     } - } -  the voidCopack (intSpaintval) - { -     inti; -      for(i = spa; I <= M; + +)i) { +Dp[i] = max (Dp[i-spa] +Val, dp[i]); -     } + } A  at voidMultipack (intSpaintValintnum) - { -     //can ignore the quantity when the full backpack -     if(M <= Spa *num) { - Copack (Spa, Val); -         return ; in     } -     //otherwise the binary divides the number of goods do, 01 backpack to     intK =1; +      while(K <num) { -num-=K; theZopack (Spa * k, Val *k); *  $K <<=1;Panax Notoginseng     } -Zopack (Spa * num, Val *num); the } +  A intMain () the { +      while(SCANF ("%d%d", &n, &m)! = EOF && (N | |M)) { -         //Initialize $         //memset (DP, 0, sizeof (dp[0)) * (M + 1)); //Hey, 110, why don't you give me that? RE (╯‵-′) ╯︵┻━┻ $Memset (DP,0,sizeof(DP)); -         inti; -         //input the          for(i =1; I <= N; ++i) { -scanf"%d", &val[i]);Wuyi         } the          for(i =1; I <= N; ++i) { -scanf"%d", &num[i]); Wu         } -         //do a full backpack, for each kind of item About          for(i =1; I <= N; ++i) { $ //multipack (Val[i], val[i], num[i]); - multipack (Val[i], val[i], num[i]); -         } -         //Traverse DP, Statistical solution number A         intres =0; +          for(i =1; I <= M; ++i) { the             if(Dp[i] = =i) { -++Res; $             } the         } the         //Output theprintf"%d\n", res); the     } -}

HDU 2844 Coins Multiple backpack

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.