【发布时间】:2013-05-01 03:12:40
【问题描述】:
问题是:
我想出的算法是这样的:
pair<bool, bitmask>[n][A] memo;
// memo[i][j].first will be true if its possible to
// use up to i-th denomination for amt j
// memo[i][j].second will contain info on which
// denominations are used
for i = 0 to n:
for j = 1 to A:
if (i==j): C[i][j] = {true, {i}}
else if (C[i-1][j].first == true): C[i][j] = C[i-1][j]]
else if (...see recurrance relation...):
C[i][j] = {true, C[i-1][j]+{denom[i]}}
else: C[i][j] = false
到目前为止是正确的吗?但我不确定如何证明它的正确性......我的尝试看起来只是用英文重写代码......
对于第一个如果:我们总是可以用 1 个面额 i 的硬币来解决 amt=i。对于第一个 else if:如果我们有 amt 的解决方案 使用当前面额,我们可以重复使用该解决方案。第 2 名 else if:如果可以使用当前面额(
对于复杂性:表的大小为 nA。每个单元格需要 O(1) 时间来填充。有人可以帮我指出正确的方向吗?
【问题讨论】:
-
使用文本,而不是图像。图片不可搜索。
标签: algorithm dynamic-programming asymptotic-complexity correctness