{"id":2212,"date":"2016-09-24T12:08:06","date_gmt":"2016-09-24T04:08:06","guid":{"rendered":"http:\/\/boweihe.me\/?p=2212"},"modified":"2016-09-24T12:08:06","modified_gmt":"2016-09-24T04:08:06","slug":"%e6%9c%80%e9%95%bf%e9%80%92%e5%a2%9e%e5%ad%90%e5%ba%8f%e5%88%97-%ef%bc%88%e5%8f%aa%e8%83%bd%e5%bf%bd%e7%95%a5m%e4%b8%aa%e5%85%83%e7%b4%a0%ef%bc%89","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2212","title":{"rendered":"\u6700\u957f\u9012\u589e\u5b50\u4e32 \uff08\u53ea\u80fd\u5ffd\u7565m\u4e2a\u5143\u7d20\uff09"},"content":{"rendered":"<p>\u8bb0\u5f55\u4e00\u9053\u78b0\u5230\u7684\u7b14\u8bd5\u9898\u3002<\/p>\n<h3>\u9898\u76ee<\/h3>\n<p>\u7ed9\u5b9a\u4e00\u4e2aint\u6570\u7ec4\uff0c\u6c42\u5176\u6700\u957f\u7684\u8fde\u7eed\u9012\u589e\u5b50\u5e8f\u5217\uff0c\u4f46\u9012\u589e\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u5ffd\u7565m\u4e2a\u6570\u7ec4\u5143\u7d20\u3002\u5728\u6700\u4f18\u5b50\u5e8f\u5217\u957f\u5ea6\u76f8\u540c\u65f6\uff0c\u4ee5\u5b50\u5e8f\u5217\u5143\u7d20\u4e4b\u548c\u8f83\u5c0f\u7684\u4e00\u4e2a\u4e3a\u6700\u4f18\u89e3*\u3002<br \/>\n\u5373\u5b9e\u73b0\u65b9\u6cd5<\/p>\n<pre class=\"lang:c++ decode:true \">vector&lt;int&gt; getLongestSubAry(const vector&lt;int&gt;&amp; array, int m)<\/pre>\n<h3>\u601d\u8def<\/h3>\n<p>\u516b\u6210\u662f\u52a8\u6001\u89c4\u5212\uff0c\u53c2\u8003\u6700\u957f\u8fde\u7eed\u5b50\u5e8f\u5217\u3002<br \/>\n\u9700\u8981\u5b58\u50a8\u72b6\u6001\uff1a\u7b2ci\u4e2a\u5143\u7d20\u7684{\u6700\u4f18\u5e8f\u5217\uff0c\u5ffd\u7565\u5143\u7d20\u4e2a\u6570}<br \/>\n\u4ecei\u5411\u524d\u5bfb\u627em\u4e2a\u4f4d\u7f6e\u7684\u6700\u4f18\u89e3\uff08\u56e0\u4e3a\u6700\u591a\u53ea\u80fd\u5ffd\u7565m\u4e2a\uff09\u3002\u5047\u8bbe\u90a3\u4e2a\u201c\u4e4b\u524d\u7684\u201d\u5143\u7d20\u662farray[j]\uff0c\u90a3\u5982\u679carray[i]&gt;array[j]\u5e76\u4e14array[j]+1&gt;=\u5f53\u524d\u6700\u4f18\u5e8f\u5217\u957f\u5ea6\uff0c\u5219\u5224\u65ad\u9650\u5236\u6761\u4ef6*\u4ee5\u786e\u5b9a\u662f\u5426\u66f4\u65b0\u6700\u4f18\u89e3\u3002<br \/>\n\u6574\u4e2a\u65b9\u6cd5\u7684\u590d\u6742\u5ea6\u5e94\u8be5\u662f[latex]O(n*m)[\/latex]\uff0c\u5176\u4e2dn\u4e3aarray\u7684\u957f\u5ea6\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\nstruct OptmElem{\n    vector&lt;int&gt; results;\n    int omitCount;\n    int getResultCount(){\n        return results.size();\n    }\n};\nint getSum(const vector&lt;int&gt;&amp; ary){\n    int sum = 0;\n    for(int i=0; i&lt;ary.size(); i++)\n        sum += ary[i];\n    return sum;\n}\n\/**\n* Find the longest increasing subarray\n* that allows skipping m elements\n*\/\nvector&lt;int&gt; maxSubArray(const vector&lt;int&gt;&amp; array, int m){\n    if(array.empty())\n        return vector&lt;int&gt;();\n    vector&lt;OptmElem&gt; bestResults;\n    OptmElem initOptm;\n    initOptm.results.push_back(array[0]);\n    initOptm.omitCount = m;\n    bestResults.push_back(initOptm);\n    unsigned long maxLen = 0;\n    int optiIndex = 0; \/\/index of optimal result in bestResults\n    for(int i=1; i&lt;array.size(); i++){\n        vector&lt;int&gt; currBest = {array[i]};\n        int omitCount = m;\n        for(int j=(i-m&gt;=0)?(i-m):(0); j&lt;i; j++){\n            \/\/trace back j steps\n            \/\/if omittance limit exceed, continue\n            if(bestResults[j].omitCount &lt; (i-j-1)){\n                continue;\n            }\n            if((array[i] &gt; array[j]) &amp;&amp; (bestResults[j].getResultCount()+1 &gt;= currBest.size())){\n                \/\/new optimal\n                if(bestResults[j].getResultCount()+1  == currBest.size()){\n                    \/\/on-par result, array that has the smallest sum is the best\n                    int oldSum = getSum(currBest);\n                    int newSum = getSum(bestResults[j].results) + array[i];\n                    if(newSum &gt; oldSum){\n                        continue; \/\/omit this best\n                    }\n                }\n                currBest = bestResults[j].results;\n                currBest.push_back(array[i]);\n                omitCount = bestResults[j].omitCount - (i-j-1);\n            }\n        }\n        OptmElem elem;\n        elem.results = currBest;\n        elem.omitCount = omitCount;\n        bestResults.push_back(elem);\n        if(maxLen &lt; currBest.size()){\n            maxLen = currBest.size();\n            optiIndex = i;\n        } else if (maxLen == currBest.size()){\n            int oldBestSum = getSum(bestResults[optiIndex].results);\n            int currBestSum = getSum(currBest);\n            if(currBestSum &lt; oldBestSum){\n                optiIndex = i;\n            }\n        }\n    }\n    return bestResults[optiIndex].results;\n}<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bb0\u5f55\u4e00\u9053\u78b0\u5230\u7684\u7b14\u8bd5\u9898\u3002 \u9898\u76ee \u7ed9\u5b9a\u4e00\u4e2aint\u6570\u7ec4\uff0c\u6c42\u5176\u6700\u957f\u7684\u8fde\u7eed\u9012\u589e\u5b50\u5e8f\u5217\uff0c\u4f46\u9012\u589e\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u5ffd\u7565m\u4e2a\u6570\u7ec4\u5143\u7d20\u3002\u5728\u6700\u4f18\u5b50\u5e8f\u5217\u957f\u5ea6\u76f8\u540c\u65f6\uff0c\u4ee5\u5b50\u5e8f\u5217\u5143\u7d20\u4e4b\u548c\u8f83\u5c0f\u7684\u4e00\u4e2a\u4e3a\u6700\u4f18\u89e3*\u3002 \u5373\u5b9e\u73b0\u65b9\u6cd5 vector&lt;int&gt; getLongestSubAry(const vector&lt;int&gt;&amp; array, int m) \u601d\u8def \u516b\u6210\u662f\u52a8\u6001\u89c4\u5212\uff0c\u53c2\u8003\u6700\u957f\u8fde\u7eed\u5b50\u5e8f\u5217\u3002 \u9700\u8981\u5b58\u50a8\u72b6\u6001\uff1a\u7b2ci\u4e2a\u5143\u7d20\u7684{\u6700\u4f18\u5e8f\u5217\uff0c\u5ffd\u7565\u5143\u7d20\u4e2a\u6570} \u4ecei\u5411\u524d\u5bfb\u627em\u4e2a\u4f4d\u7f6e\u7684\u6700\u4f18\u89e3\uff08\u56e0\u4e3a\u6700\u591a\u53ea\u80fd\u5ffd\u7565m\u4e2a\uff09\u3002\u5047\u8bbe\u90a3\u4e2a\u201c\u4e4b\u524d\u7684\u201d\u5143\u7d20\u662farray[j]\uff0c\u90a3\u5982\u679carray[i]&gt;array[j]\u5e76\u4e14array[j]+1&gt;=\u5f53\u524d\u6700\u4f18\u5e8f\u5217\u957f\u5ea6\uff0c\u5219\u5224\u65ad\u9650\u5236\u6761\u4ef6*\u4ee5\u786e\u5b9a\u662f\u5426\u66f4\u65b0\u6700\u4f18\u89e3\u3002 \u6574\u4e2a\u65b9\u6cd5\u7684\u590d\u6742\u5ea6\u5e94\u8be5\u662f[latex]O(n*m)[\/latex]\uff0c\u5176\u4e2dn\u4e3aarray\u7684\u957f\u5ea6\u3002 \u4ee3\u7801 #include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; struct OptmElem{ vector&lt;int&gt; results; int omitCount; int getResultCount(){ return results.size(); } }; int getSum(const vector&lt;int&gt;&amp; ary){ int sum = 0; for(int i=0; i&lt;ary.size(); i++) sum += ary[i]; return sum; } \/** * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[141],"class_list":["post-2212","post","type-post","status-publish","format-standard","hentry","category-study","tag-141"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2212"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2212\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}