Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3732
Test instructions: Ahui learning English words, each word is different, and has its own value of W and the difficulty of C (0<=w,c<=10), Ahui now given the total number of words N and can be the total difficulty value V, to maximize the value.
Analysis: 01 knapsack complexity is O (NV), this will time out, due to the small w,c, so that in n<=100000 items there must be a lot of value and difficulty of the same words, thus can become the most (11*11) kinds of goods, each item has x, Find out how much value a backpack can fit in a V-capacity. This translates into a binary optimized multi-pack, which is O (V*log (N)).
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<stack>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 100010#defineCLR (a) (Memset (A,0,sizeof (a)))using namespacestd;intValue[n],weight[n],dp[n];inthash[ One][ One],n,v,tot;intsolve () {CLR (DP); for(intI=1; i<tot;i++) { for(intj=v;j>=weight[i];j--) Dp[j]=max (dp[j-weight[i]]+Value[i],dp[j]); } returndp[v];}intMain () { while(SCANF ("%d%d", &n,&v) >0) {clr (hash); for(intI=1; i<=n;i++) { Chars[ -]; intx, y; scanf ("%s%d%d",s,&x,&y); Hash[x][y]++; } tot=1; for(intI=0; i<=Ten; i++) for(intj=0; j<=Ten; j + +) { if(Hash[i][j]) { for(intk=1; k<=hash[i][j];k*=2) {Value[tot]=i*K; Weight[tot++]=j*K; HASH[I][J]-=K; } if(Hash[i][j]) value[tot]=hash[i][j]*i,weight[tot++]=hash[i][j]*J; } } intans=solve (); printf ("%d\n", ans); }}View Code
hdu3732 (multiple backpack)