diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-08-06 08:15:45 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2013-08-06 08:15:45 +0200 |
commit | a2b0bb41876d956ca7abf123858ac45aea3c5898 (patch) | |
tree | 63dccf89aa4690c770196b6e49a974ca7837d3df /01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch | |
parent | b14e17eaa3cf971800eec93ca2f1bc5582252a88 (diff) | |
download | coursera-descrete.zip coursera-descrete.tar.gz |
Discrete : wip-worktree-statedescrete
Diffstat (limited to '01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch')
-rw-r--r-- | 01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch b/01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch new file mode 100644 index 0000000..bb4a6e1 --- /dev/null +++ b/01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch @@ -0,0 +1,38 @@ +diff --git a/01-knapsack/ks_dp-ng.c b/01-knapsack/ks_dp-ng.c +index 7830651..106328b 100644 +--- a/01-knapsack/ks_dp-ng.c ++++ b/01-knapsack/ks_dp-ng.c +@@ -120,20 +120,32 @@ static void solve(Solver* solver) + + // this computes the top-left bottom-right diagonal, + // values above this don't need to be computed ++ j = k; + min_c -= w; + lower_bound = (k - min_c); + if (lower_bound < w) + lower_bound = w; +- for (j = k; j >= lower_bound; j--, data--, up_left--) ++ ++ while (j >= lower_bound) + { + counter++; ++ /* printf(" % 4d\n", j); */ + + if (data->v < (v + up_left->v)) + { + data->i = i; + data->v = v + up_left->v; + data->w = w + up_left->w; ++ if (j == k) { ++ j -= w; ++ data -= w; ++ up_left -= w; ++ continue; ++ } + } ++ j--; ++ data--; ++ up_left--; + } + + if (debug > 2) |