{"id":5377,"date":"2021-07-09T17:03:06","date_gmt":"2021-07-09T09:03:06","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=5377"},"modified":"2021-07-20T12:40:37","modified_gmt":"2021-07-20T04:40:37","slug":"%e8%87%aa%e5%8a%a8%e5%94%ae%e8%b4%a7%e6%9c%ba%e4%b9%b0%e4%b8%9c%e8%a5%bf","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5377","title":{"rendered":"\u81ea\u52a8\u552e\u8d27\u673a\u4e70\u4e1c\u897f"},"content":{"rendered":"<p>20210720\uff0c\u4eca\u5929\u91cd\u505a\u4e86\uff0c\u4e4b\u524d\u505a\u7684\u7b80\u76f4\u8ddfS\u4e00\u822c\uff0c\u8fd9\u79cd\u9898\u76ee\u5f97\u7528dp+cache\u554a\uff0cms\u771f\u662f\u4e00\u5bb6\u4e86\u4e0d\u8d77\u7684\u516c\u53f8\uff0c\u5bf9\u4eba\u53cb\u597d\uff0c\u9898\u76ee\u6709\u610f\u601d<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;map&gt;\r\n\r\n\/*\r\n\u7528\u6700\u5c11\u6570\u91cf\u7684\u94b1\u4e70\u5546\u54c1\r\n*\/\r\n\r\nclass Solution {\r\npublic:\r\n    std::vector&lt;int&gt; buy_goods(const std::vector&lt;int&gt;&amp; money, int price) {\r\n        std::map&lt;int, std::vector&lt;int&gt;&gt; cache;\r\n        return dp(money, price, cache);\r\n    }\r\n\r\n    std::vector&lt;int&gt; dp(const std::vector&lt;int&gt;&amp; money, int price, std::map&lt;int, std::vector&lt;int&gt;&gt;&amp; cache) {\r\n        if (price &lt;= 0)\r\n            return std::vector&lt;int&gt;();\r\n\r\n        for (auto i : money) {\r\n            if (price == i) {\r\n                return std::vector&lt;int&gt;(1, i);\r\n            }\r\n        }\r\n\r\n        auto it = cache.find(price);\r\n        if (it != cache.end())\r\n            return it-&gt;second;\r\n\r\n        std::vector&lt;int&gt; result;\r\n        for (auto i : money) {\r\n            auto this_result = dp(money, price - i, cache);\r\n            if (this_result.size() == 0)\r\n                continue;\r\n\r\n            if (result.size() == 0) {\r\n                result = this_result;\r\n                result.push_back(i);\r\n            } else if (this_result.size() &lt; result.size()) {\r\n                result = this_result;\r\n                result.push_back(i);\r\n            }\r\n        }\r\n\r\n        cache[price] = result;\r\n        return result;\r\n    }\r\n};\r\n\r\nint main() {\r\n    std::vector&lt;int&gt; money;\r\n    for (int i = 0; i &lt; 3; i++)\r\n        money.push_back(100);\r\n    for (int i = 0; i &lt; 3; i++)\r\n        money.push_back(10);\r\n    for (int i = 0; i &lt; 3; i++)\r\n        money.push_back(5);\r\n    for (int i = 0; i &lt; 3; i++)\r\n        money.push_back(2);\r\n    auto result = Solution().buy_goods(money, 32);\r\n    return 0;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e4b\u524d\u7528\u6392\u5217\u7684\u65b9\u5f0f\u6765\u505a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\u5230\u6781\u70b9<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>20210720\uff0c\u4eca\u5929\u91cd\u505a\u4e86\uff0c\u4e4b\u524d\u505a\u7684\u7b80<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[120],"class_list":["post-5377","post","type-post","status-publish","format-standard","hentry","tag-04-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5377"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5377\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}