butterflies's blog

By butterflies, history, 2 years ago, In English

EDIT: After talking to another person, I understand that if dp[k] gets updated by a certain cow, then that same cow CANNOT be used to update dp[k1]. So, this means we do not have to worry about updating dp[k1] if dp[k] gets updated.

Problem link: http://www.usaco.org/index.php?page=viewproblem2&cpid=839

Solution: http://www.usaco.org/current/data/sol_talent_gold_open18.html

In this problem, I am confused by this part in the solution

for (int j = 0; j < n; j++) {
    long long value = 1000*(long long)talent[j] - y*(long long)weights[j];
    int inc = weights[j];
    for (int k = w; k >= 0; k--) {
      int k1 = min(w, k + inc);
      if (dp[k] != -infinite) {
        if (dp[k1] < dp[k] + value) {
          dp[k1] = dp[k] + value;
        }
      }
    }
  }

The situation I’m having trouble on is if dp[k] gets updated by another cow, will dp[k1] also be updated?

First, if $$$k1 = W$$$ then we know that dp[W] will get updated for each cow.

Suppose the cow that updates dp[k] has weight $$$w$$$. Then, we know the value of dp[k - w] must be defined.

If dp[k - w] was defined BEFORE we visited the cow with weight $$$k1 - k$$$, then dp[k - w + k1 - k] = dp[k1 - w] gets updated.

Otherwise, dp[k - w] gets defined AFTER we visit the cow with weight $$$k1 - k$$$. We know dp[k1 - k] is defined, but what if dp[k1 - k] was used to compute dp[k - w]. How can we guarantee dp[k1 - w] is defined in this situation?

Thanks for the help!

Edit: bump

  • Vote: I like it
  • +7
  • Vote: I do not like it

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

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

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

Note that the order in which cows are added to the set doesn't matter. Therefore, in the case you describe, it doesn't matter whether $$$dp_{k_1 - w}$$$ is defined because if it hasn't been defined yet, whichever cows we would use to define it will be processed later and integrated into the DP correctly.

More formally, that the optimal set of cows is $$${c_1, c_2, \cdots, c_m }$$$ for some $$$m$$$. Then, we can show by induction that after processing cow $$$c_i$$$, the value of $$$dp_{c_1 + \cdots + c_i}$$$ is set to the sum of the values of cows $$$c_1, \cdots, c_i$$$ (since clearly, this value is possible, and no larger value is possible because that would imply that a different set achieves a better result). This implies that after processing cow $$$c_m$$$, the value of $$$dp_{c_1 + \cdots + c_m} = dp_w$$$ is equal to the value of the entire set $$${c_1, c_2, \cdots, c_m },$$$ which is our optimal answer.

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

    Hi, sorry for the late reply.

    For your formal proof, if $$$c_1, \cdots, c_{i - 1}$$$ is the optimal value for $$$dp_{c_1 + \cdots + c_{i - 1}}$$$, why does $$$c_1, \cdots, c_i$$$ have to be the optimal value for $$$dp_{c_1 + \cdots + c_i}?$$$ This is the idea I'm getting from your induction.

    I understand that if your induction is correct, then we guarantee that the value of $$$dp_{c_1 + \cdots + c_m} = dp_w$$$ will be set to the optimal value.

    Thanks!

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

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