{"id":325,"date":"2019-10-05T17:12:43","date_gmt":"2019-10-05T09:12:43","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=325"},"modified":"2019-10-05T17:12:43","modified_gmt":"2019-10-05T09:12:43","slug":"poj-3253-fence-repair","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=325","title":{"rendered":"POJ 3253 \u2014 Fence Repair"},"content":{"rendered":"<h3>Description<\/h3>\n<p>Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 \u2264 N \u2264 20,000) planks of wood, each having some integer length Li (1 \u2264 Li \u2264 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the &#8220;kerf&#8221;, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.<\/p>\n<p>FJ sadly realizes that he doesn&#8217;t own a saw with which to cut the wood, so he mosies over to Farmer Don&#8217;s Farm with this long board and politely asks if he may borrow a saw.<\/p>\n<p>Farmer Don, a closet capitalist, doesn&#8217;t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.<\/p>\n<p>Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.<\/p>\n<h3>Input<\/h3>\n<p>Line 1: One integer N, the number of planks<br \/>\nLines 2..N+1: Each line contains a single integer describing the length of a needed plank<\/p>\n<h3>Output<\/h3>\n<p>Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts<\/p>\n<h3>Sample Input<\/h3>\n<pre><code class=\"language-cpp line-numbers\">3\n8\n5\n8\n<\/code><\/pre>\n<h3>Sample Output<\/h3>\n<p><code>34<\/code><\/p>\n<h3>Hint<\/h3>\n<p>He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.<br \/>\nThe original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).<\/p>\n<h3>\u9898\u76ee\u94fe\u63a5<\/h3>\n<p><a href=\"http:\/\/poj.org\/problem?id=3253\" title=\"http:\/\/poj.org\/problem?id=3253\">http:\/\/poj.org\/problem?id=3253<\/a><\/p>\n<h3>\u9898\u76ee\u7ffb\u8bd1<\/h3>\n<p>\u519c\u592b\u7ea6\u7ff0\u4e3a\u4e86\u4fee\u7406\u6805\u680f\uff0c\u8981\u5c06\u4e00\u5757\u5f88\u957f\u7684\u6728\u677f\u5207\u5272\u6210N\u5757\u3002\u51c6\u5907\u5207\u6210\u7684\u6728\u677f\u957f\u5ea6\u4e3aL1\uff0cL2\uff0cL3\u2026\u2026LN\uff0c\u672a\u5207\u5272\u524d\u6728\u677f\u7684\u957f\u5ea6\u6070\u597d\u4e3a\u5207\u5272\u540e\u6728\u677f\u957f\u5ea6\u7684\u603b\u548c\u3002\u6bcf\u6b21\u5207\u65ad\u6728\u677f\u65f6\uff0c\u9700\u8981\u7684\u5f00\u9500\u4e3a\u8fd9\u5757\u6728\u677f\u7684\u957f\u5ea6\u3002\u8bf7\u6c42\u51fa\u6309\u7167\u76ee\u6807\u8981\u6c42\u5c06\u6728\u677f\u5207\u5272\u5b8c\u7684\u6700\u5c0f\u5f00\u9500\u662f\u591a\u5c11\uff1f\u4f8b\u5982\u957f\u5ea6\u4e3a21\u7684\u6728\u677f\u5207\u5272\u6210\u957f\u5ea6\u4e3a13\u548c8\uff0c\u5f00\u9500\u4e3a21\uff1b\u628a\u957f\u5ea6\u4e3a13\u7684\u6728\u677f\u5207\u5272\u62105\u548c8\uff0c\u5219\u5f00\u9500\u4e3a13\uff0c\u6240\u4ee5\u5c06\u957f\u5ea6\u4e3a21\u7684\u6728\u677f\u5207\u5272\u62108\uff0c5,8\u7684\u4e09\u5757\uff0c\u5f00\u9500\u662f34.<br \/>\n1 &lt;= N &lt;= 20000\uff0c0 &lt;= Li &lt;=50000<\/p>\n<h3>\u8f93\u5165<\/h3>\n<p>\u7b2c\u4e00\u884c\u4ec5\u4e00\u4e2a\u6b63\u6574\u6570N\uff0c\u7b2c\u4e8c\u884c\u6709N\u4e2a\u6b63\u6574\u6570\uff0c\u4e24\u4e24\u4e4b\u95f4\u7528\u4e00\u4e2a\u7a7a\u683c\u5206\u9694\u3002<\/p>\n<h3>\u8f93\u51fa<\/h3>\n<p>\u7b26\u5408\u9898\u76ee\u8981\u6c42\u7684\u4e00\u4e2a\u6b63\u6574\u6570<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<p>\u5f97\u5230\u6700\u7ec8\u7684\u4e00\u5757\u6728\u677f\u7684\u5f00\u9500\u4e3a\uff1a\u6728\u677f\u7684\u957f\u5ea6 \u00d7 \u6240\u9700\u7684\u5207\u5272\u6b21\u6570<br \/>\n\u56e0\u6b64\uff0c\u6700\u4f18\u7684\u5207\u5272\u65b9\u6cd5\u5c31\u662f\u4ee4\u77ed\u6728\u677f\u6240\u9700\u7684\u5207\u5272\u6b21\u6570\u591a\uff0c\u957f\u6728\u677f\u6240\u9700\u7684\u5207\u5272\u6b21\u6570\u5c11\u3002<br \/>\n\u7c7b\u4f3c\u4e8e\u6700\u4f18\u4e8c\u53c9\u6811\u3002<br \/>\n\u6784\u9020\u4e00\u4e2a\u4f18\u5148\u961f\u5217\uff0c\u5148\u5c06\u6240\u6709\u6728\u677f\u7684\u957f\u5ea6\u653e\u8fdb\u53bb\u3002\u6bcf\u6b21\u53d6\u6700\u5c0f\u7684\u4e24\u4e2a\u6570\uff0c\u5c06\u4e24\u6570\u4e4b\u548cSUM\u653e\u8fdb\u961f\u5217\uff0c\u603b\u5f00\u9500\u52a0\u4e0aSUM\u3002\u76f4\u5230\u961f\u5217\u4e2d\u53ea\u6709\u4e00\u4e2a\u6570\u5b57\uff0c\u5c31\u53ef\u4ee5break\u4e86\u3002<br \/>\n\u76f8\u5f53\u4e8e\u6a21\u62df\u4e86\u7528\u5206\u6563\u7684\u6728\u677f\u5408\u5e76\u6210\u4e3a\u6574\u5757\u6728\u677f\u7684\u8fc7\u7a0b\u3002<br \/>\n\u53e6\u5916\uff0c\u4f7f\u7528 int \u662f\u4e0d\u884c\u7684\uff0c\u56e0\u4e3a\u5728\u6728\u677f\u6570\u91cf\u6bd4\u8f83\u591a\u65f6\uff0c\u603b\u5f00\u9500\u8fdc\u5927\u4e8e\u6574\u5757\u6728\u677f\u7684\u957f\u5ea6\u3002<\/p>\n<h3>AC\u4ee3\u7801<\/h3>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;queue&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\npriority_queue&lt;long long, vector&lt;long long&gt;, greater&lt;long long&gt; \/**\/&gt; q;\n\nint main()\n{\n    int n, i, l;\n    long long ans = 0;\n    cin &gt;&gt; n;\n    for (i = 0; i &lt; n; i++)\n    {\n        cin &gt;&gt; l;\n        q.push(l);\n    }\n    while (q.size() &gt; 1)\n    {\n        long long x = q.top();\n        q.pop();\n        long long y = q.top();\n        q.pop();\n        q.push(x + y);\n        ans += x + y;\n    }\n    cout &lt;&lt; ans &lt;&lt; endl;\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Description Farmer John wants to repair a small length  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[25,27,33,51,62],"class_list":["post-325","post","type-post","status-publish","format-standard","hentry","category-algo","tag-poj","tag-stl","tag-33","tag-51","tag-62"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=325"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/325\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}