hdu2844 Multiple Backpack templates

Source: Internet
Author: User

01 Backpack

There are n different items, each item has two attributes, size volume, value value, now to a backpack with a capacity of W, ask the most can take away how much value of the goods.

int f[w+1];   // F[x] Indicates the maximum value  when the backpack capacity is X  for (int i=0; i<n; i++)        for (int j=w; j>=size[i]; j--)           // Reverse

Full backpack

If the item does not count as many items as there are countless pieces, change it slightly.

 for (int i=0; i<n; i++)        for (int j=size[i]; j<=w; j + +)           = Max (F[j], f[j-size[i]]+value[i]);  // Positive Order

Multiple backpacks are both a certain weight W and value V for each object, and a certain amount of CNT, set m for the backpack can contain weight;

#include <iostream>#include<map>#include<math.h>#include<algorithm>#include<vector>#include<cstdlib>#include<cstdio>#include<cstring>#include<Set>using namespacestd;intn,m,a[ the],num[ the],dp[100005];voidCOMDP (intWintv) {    inti;  for(i=w; i<=m; i++) Dp[i]=max (dp[i],dp[i-w]+v);}voidZeroOne (intWintv) {    inti;  for(i=m; i>=w; i--) Dp[i]=max (dp[i],dp[i-w]+v);}voidMULTIDP (intWintVintCNT)//Start multiple backpacks at this time, Dp[i] indicates the maximum value that is included in the backpack weight of I {if(cnt*w>=m)//this is equivalent to an unlimited number of items. Complete Backpack{COMDP (w,v); return; }    intk=1;//Otherwise, the multi-backpack, the code under the mathematical theorem can be     while(k<=CNT) {ZeroOne (k*w,k*v); CNT-=K; K*=2; } zeroone (CNT*w,cnt*v); return ;}

Theorem: A positive integer n can be decomposed into 1,2,4,..., 2^ (k-1), n-2^k+1 (k is the largest integer that satisfies n-2^k+1>0), and all integers within 1~n can be uniquely represented as 1,2,4,..., 2^ (k-1), n-2^k+ 1 in the form of a certain number of numbers.

The proof is as follows:

(1) Sequence 1,2,4,..., 2^ (k-1), and the N of all elements in n-2^k+1, so the range of several elements is: [1, N];

(2) If the positive integer t<= 2^k–1, then T must be able to use 1,2,4,..., 2^ (k-1) Some number of the and expression, this is easy to prove: We put the binary representation of T, it is obvious that T can be expressed as n=a0*2^0+a1*2^1+...+ak*2^ (k-1), where ak=0 or 1, indicates that the AK bit of T is a binary number 0 or 1.

(3) If t>=2^k, set s=n-2^k+1, then t-s<=2^k-1, so T-S can be expressed as 1,2,4,..., 2^ (k-1) in the form of some number of, and then T can be expressed as 1,2,4,..., 2^ (k-1), The form of a certain number of numbers in S and (Addend must contain s).

(Certificate of Completion!) )

hdu2844 Multiple Backpack templates

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.