Bzoj 1190 HNOI2007 Fantasy Island Orb Dynamic Planning

Source: Internet
Author: User

Title: 01 Backpack, which weight<=2^30, but each weight can be written in a*2^b form, of which a<=10,b<=30

Direct backpack sure Tle+mle

Considering that every weight can be written in a*2^b form, obviously we're going to do the backpack in B-tiered.

F[I][J] represents the maximum value for space with j*2^i+ (w& (1<<i)-1)

First, make a 01 backpack inside each layer.

Then transfer between layers and layers

from large to small enumeration J transfer equation is F[i][j]=max{f[i][j],f[i][j-k]+f[i-1][min (k*2+ ((w>>i-1) &1), 1000)]}

How did this come about? We first enumerate J, and then enumerate the space j-k of the same layer, then we can choose k*2+ ((w>>i-1) &1) on the previous level.

It's hard to understand? Give me a chestnut.

w= (1000100010) 2,i=6,j= (111) 2,k=1

So what we're going to update is (111100010) 2, to update this value is (110000000) 2

K=1, representing the difference between the layer 1, the difference between the previous layer 2

((w>>i-1) &1=1, on behalf of the previous layer can also choose 1 (6th position on the 1)

We will choose (1100010) 2 of the space at the next level, and this value is recorded on f[5][3]

Or understand not can only see a few more times this paragraph = = The problem is so damn rational and pleasant

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;int N,w;long Long F[40][1010],ans;int Main () {int I,j,k,x;while (cin>>n>>w,n>0) {memset (f,0,sizeof F ); Ans=0;for (i=1;i<=n;i++) {int a=0,b=0;scanf ("%d%d", &a,&x), while (~a&1) a>>=1,++b;for (j=1000; j>=a;j--) F[b][j]=max (f[b][j],f[b][j-a]+x);} for (i=0;i<=30;i++) for (j=1;j<=1000;j++) F[i][j]=max (F[i][j],f[i][j-1]), for (I=1;i<=min (1000,w); i++) ans= Max (Ans,f[0][i]); for (i=1;i<=30&& (1<<i) <=w;i++) for (J=min (1000,w>>i), ~j;j--) {for (k=0;k <=j;k++) F[i][j]=max (F[i][j],f[i][j-k]+f[i-1][min (k+k+ (w>>i-1) &1)]); Ans=max (Ans,f[i][j]);} Cout<<ans<<endl;} return 0;}


Bzoj 1190 HNOI2007 Fantasy Island orb Dynamic planning

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.