Nyoj 1091 Oversized 01 backpack (binary enumeration)

Source: Internet
Author: User

This problem at first glance is a common 01 backpack, the most basic, but carefully look at the data, found that the ordinary can not do, carefully observed array found n relatively small, using this feature will be divided into the first half and the second half of this good, at that time in the Internet to find the problem, not found, and later in the Challenge program design found this question Just take a quote.

The method of selecting items always from 2^n, direct enumeration is definitely not, because N maximum is 40, but if N is 20, it is necessary to use the binary enumeration, the first half of enumeration, after enumeration half. The first part of the selection method corresponding to the weight and value of the sum of W1, v1, so that the second half of the search W2 <= w-w1 when v2 the largest selection method is good.

Therefore, we want to consider the method of efficiently finding max{v2|w2<=w '} from the collection of enumerations (W2, V2). First of all, obviously we can exclude all W2[i] <= w2[j] and V2[i] >= V2[j] J. This can be done according to W2,v2 's dictionary order. Then the remaining elements satisfy W2[i] < W2[J], V2[i] < v2[j], to calculate max{v2|w2<=w '}, just look for the largest I to meet W2[i]<=w '. This can be done using a two-point search. The number of elements remaining is M, a search to use log (M) time, you can solve

The code is as follows:

Method One (binary enumeration):

1  2#include <iostream>3#include <algorithm>4#include <cstdio>5 #defineMax (A, b) a>b?a:b6 #defineINF 100000000000000007 using namespacestd;8typedefLong LongLL;9 Const intMAX = +;Ten LL Weight[max], Value[max]; One LL W; APair<ll, ll> ps[1<< (MAX/2)]; - intN; - voidSlove () the { -     //enumerating the first half -     intN2 = N/2; -      for(inti =0; I <1<< N2; i++)//the total number of enumerations in the first half is 2^ (N/2); +     { -LL SW =0, SV =0; +         //Each result selects a specific value and weight (i.e altogether 2 things, there are altogether four cases, do not choose, choose the first one, choose the second one, choose) A          for(intj =0; J < N2; J + +) at         { -             if(I >> J &1) -             { -SW + =Weight[j]; -SV + =Value[j]; -             } in         } -Ps[i] = Make_pair (SW, SV);//adding to the PS array to     } +     //sort the PS -Sort (PS, PS + (1<<n2)); the     //PS de-weight *     intm =1; $      for(inti =1; I <1<< N2; i++)Panax Notoginseng         if(Ps[m-1].second <Ps[i].second) -ps[m++] =Ps[i]; theLL res =0;//Save Results +     //enumerates the latter half and finds the optimal solution A      for(inti =0; I <1<< (N-N2); i++)//the total number of identical enumerations the     { +LL SW =0, SV =0; -          for(intj =0; J < N-n2; J + +)//same as the first half. $         { $             if(I >> J &1) -             { -SW + = Weight[n2 +j]; theSV + = value[n2 +j]; -             }Wuyi         } the         if(SW <= W)//Add a judgment to solve the maximum value, only less than the time of the backpack capacity -         { WuLL TV = (Lower_bound (PS, PS + M, Make_pair (W-SW, INF))-1)->second;//find the value corresponding to the first half of the section -res = Max (res, SV +TV);  About         } $     } -printf"%lld\n", res); - } -  A intMain () + { the      while(~SCANF ("%d%lld", &n, &W)) -     { $          for(inti =0; I < n; i++) thescanf"%lld%lld", &weight[i], &value[i]); the slove (); the     } the     return 0; - } in         

This problem can also be used to do, search instead come faster, because N is smaller

Method Two (search):

1#include <stdio.h>2#include <string.h>3 #defineMax (A, b) a > B? A:b4 Const intMAX = $;5 Long LongWeight[max], Value[max], Sw[max], Sv[max];6 Long LongW, n, ans;7 //I represents the current fetch to N-i, CNT represents the current total value, W current backpack remaining space8 voidDfsintILong LongCntLong LongW)9 {Ten     if(i = =0)//take the last One     { AAns =Max (ans, CNT); -         return; -     } the     if(W = =0|| CNT + Sv[i] < ans)//The total value of the backpack full or current total plus this first I is less than the current total values, this step is pruning -         return ; -     if(w >= Sw[i])//because it is from the top down, so as long as the current capacity can be loaded with the first I and, so this must be the largest -     { +CNT + =Sv[i]; -Ans =Max (ans, CNT); +W =0; A         return ;  at     } -     if(W > Weight[i])//Deep search of two states -DFS (I-1, CNT + value[i], w-weight[i]);//the equivalent of two states in a 01 backpack -DFS (I-1, CNT, W); - }  - intMain () in { -      while(~SCANF ("%d%lld", &n, &W)) to     { +memset (SW,0,sizeof(weight)); -memset (SV,0,sizeof(value)); theAns =0; *          for(inti =1; I <= N; i++) $         {Panax Notoginsengscanf"%lld%lld", &weight[i], &value[i]); -Sw[i] = sw[i-1] +Weight[i]; theSv[i] = sv[i-1] +Value[i]; +         } ADFS (N,0, W); theprintf"%lld\n", ans); +     } -      $     return 0; $}

Nyoj 1091 Oversized 01 backpack (binary enumeration)

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.