hdu3732 (multiple backpack)

Source: Internet
Author: User

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)

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.