Блог пользователя jlcastrillon

Автор jlcastrillon, 14 лет назад, По-английски

This problem looks like knapsack with repetition but limits are huge and I can't find a solution using DP, Could anyone tell me how to solve it, here is the link. http://poj.org/problem?id=3900

I have seen this kind of problems several times but I have never been able to solve them. Thanks in advance....

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
14 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

N is not too big, so you can test all subsets of a given set. For each subset you can calculate its weight and cost in linear time. It's a O(N·2N) per test solution, and it seems to be OK.

UPD: that's not a solution, I didn't get the problem corerctly.

»
14 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

But how if each element can be taken several times...

  • »
    »
    14 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +1 Проголосовать: не нравится

    Sorry, I misread the statement and was wrong. So, in the correct problem N is less or equal then 120.

    I don't think that there exists a polynomial solution. Maybe meet in the middle or something, but I can't see get it right now. Anyway, maybe bactracking with good pruning and greedy optimisations, simulated annealing or other heuristics like these can get AC (but not solve the problem).

    • »
      »
      »
      14 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +6 Проголосовать: не нравится

      120? Correct problem? Please explain this moment, i don't get it.
      Meet-in-the-middle is simple, but too slow. we can divide diamonds into two groups 1..10 and 11..15, for each of the groups brute-force answers, then sort two arrays of pairs (weight, cost). now we can find in linear time (using two's pointers method) two elements with total weight less than or equal to M and with maximal cost.

»
14 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

I don't know for sure, and i can't prove this, but it seems to be true: we must to find solution for the case when we steal something that can be divided as we want (for example different types of the sand) solve problem for this case (it's easy, we must to take the most expensive sand as many as we can). Now solutions for this problem differs only by +/-1 for each type. Let we have solution S for "sand problem". S[i] means how many sand of type I must be taken. We want to know real solution R, i think R[i] is equal to Floor(S[i]) or Ceil(S[i]). Floor(X) is the nearest integer less than or equal to X, Ceil(X) is the nearest integer greater than or equal to X.

»
14 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This problem seems hard,but actually just simply search with some insightful pruning ( and reorder the items by some greedy idea) can succeed.