HDU 2844 Coins (multiple backpack)

Source: Internet
Author: User



Test instructions: There are n denominations of coins a[i], each coin has c[i], asked to be composed of not less than the number of M (1~m).


Multi-Backpack Template: by backpack nine speaking.



#include <cstdio> #include <stdlib.h> #include <string.h> #include <string> #include <map > #include <cmath> #include <iostream> #include <queue> #include <stack> #include < Algorithm> #include <set>using namespace std; #define INF 1e8#define EPS 1e-8#define LL long long#define MAXN 10000 1#define mol 1000000007//procedure multiplepack (cost,weight,amount)//If cost*amount>=v//CompletePack (cost , weight)//return//integer k=1//while k<amount//zeroonepack (k*cost,k*weight)//Amoun t=amount-k//k=k*2//zeroonepack (amount*cost,amount*weight) int n,m,a[110],c[110];int dp[maxn];void COMPLETEPAC K (int cost,int weight)//Total backpack {for (int v=cost;v<=m;v++) Dp[v]=max (dp[v],dp[v-cost]+weight);} void zeroonepack (int cost,int weight)//01 backpack {for (int v=m;v>=cost;v--) Dp[v]=max (dp[v],dp[v-cost]+weight);} void Multiplepack (int cost,int weight,int amount) {if (cost*amount>=m) completepack (cost,weight); else {int K=1;while (k<amount)//binary split {zeroonepack (k*cost,k*weight); amount=amount-k; k=k*2;} Zeroonepack (Amount*cost,amount*weight);}} int main () {while (scanf ("%d%d", &n,&m)) {if (n==0&&m==0) break;for (int i=1;i<=n;i++) scanf ("%d", &a[i]), for (int i=1;i<=n;i++) scanf ("%d", &c[i]), and for (int i=1;i<=m;i++) dp[i]=0;for (int i=1;i<=n;i++) Multiplepack (A[i],a[i],c[i]), int ans=0;for (int i=1;i<=m;i++) if (dp[i]==i) ans++;p rintf ("%d\n", ans);} return 0;} /*3 101 2 4 2 1 12 51 4 2 1*/


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.