Master_coder's blog

By Master_coder, history, 6 years ago, In English

i want to know how to optimize classic 0/1 knapsack to 0/k knapsack . the problem link is https://open.kattis.com/problems/thief

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

a naive solution would involve storing k, and looping k times on an item during the knapsack this does not work for cases where k is large

however, 0/k knapsack can be transformed into 0/1 knapsack, by making big items which are 2^X items combined together, since binary can be used to form any number up to a point

i have adapted a paragraph from singapore's national olympiad in informatics preliminary round writeup

Notice that we can group the K copies into O(log K) groups where the sizes are powers of 2 except the last one. For example, if K = 25, we can group into 5 groups of size 1, 2, 4, 8, 10 respectively. Notice that the size of the groups add up to 25 and also any number from 1 to 25 can be represented by a sum of some subset of the above groups. Thus, by considering subsets of these groups, we would have considered every possibility of taking 1, 2, 3 ..., K items.

this makes it efficient as there are log k items instead of k of them

after this, normal knapsack can be applied on the new items formed

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    still this solution not work it exceed time limit . we can do this in O(knapsack_size*num_of_items) i am getting how to do this so can any body help me ??

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      This question isn't asking for 0/k knapsack where up to k items are taken

      As I replied before you deleted my reply, you can use bottom up DP such as the one found on geeksforgeeks where values are calculated for all knapsack sizes

»
6 years ago, # |
  Vote: I like it +6 Vote: I do not like it

I don't think that this question simply asks for 0/k knapsack (or bounded knapsack, the popular name). You can check here and here for a better explanation.