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

Автор mustard_with_fries69420, история, 10 месяцев назад, По-английски

I have been trying to fix the bug in my code for this problem: https://oj.uz/problem/view/NOI18_knapsack. Any help would be very much appreciated.

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

»
10 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by mustard_with_fries69420 (previous revision, new revision, compare).

»
10 месяцев назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Sorry about the bad format of the code, I don't know how to make it better.

»
10 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone help please?

»
9 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I had a hard time understanding the way your code worked. But I think the problem is in the algorithm. If I understand your code correctly, you're trying to maximize the number of items you pick in each type of thing. But I think the lemma isn't true (Maybe there exists a proof or a counter-example(??), but I didn't try to find it). For the correct algorithm to solve the problem, this is my attempt: We call dp[i][j] as the maximum value we can achieve by choosing only the items with a weight smaller or equal to i, and the total weight is j. It's easy to see that dp[i][j] = max(dp[i-1][j-k*i] + val) (j-k*i >= 0) with val as the maximum value you can achieve by choosing k items with the weight of i (this can be easily calculated in O(1) using a simple greedy strategy). For the running time analysis, we see that to calculate dp[i][j], we have to loop through approximately i/j values of k. Summing with all i and j from 1 to n we have the number of operations required is (1+2+3+4+...+S)*(1/1+1/2+1/3+...+1/S). The first term is O(S^2), and the second term is just the harmonic series (it can be easily proved that O(1/1+1/2+1/3+...+1/S) = O(log S), but the proof is left as an exercise). So the total complexity is O(S^2 log S) with a pretty small constant, I think. Hope this help. P/s: sorry for bad quality comment and bad English skill