{"id":2853,"date":"2022-03-10T19:55:54","date_gmt":"2022-03-10T11:55:54","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2853"},"modified":"2022-03-10T19:55:56","modified_gmt":"2022-03-10T11:55:56","slug":"heapq_1","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/10\/heapq_1\/","title":{"rendered":"\u6570\u636e\u7ed3\u6784 &#8212; \u5806"},"content":{"rendered":"\n<p class=\"has-light-pink-background-color has-background\">python\u4e2d\u76f4\u63a5\u4f7f\u7528heapq\u5efa\u7acb \u5c0f\u6839\u5806<\/p>\n\n\n\n<p>h<a href=\"ttp:\/\/139.9.1.231\/index.php\/2022\/02\/22\/python3heapq\/\">ttp:\/\/139.9.1.231\/index.php\/2022\/02\/22\/python3heapq\/<\/a><\/p>\n\n\n\n<h1>\u5806\u7b80\u4ecb<\/h1>\n\n\n\n<p>\u5806\u662f\u4e00\u68f5\u6811\uff0c\u5176\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e00\u4e2a\u952e\u503c\uff0c\u4e14\u6bcf\u4e2a\u8282\u70b9\u7684\u952e\u503c\u90fd\u5927\u4e8e\u7b49\u4e8e\/\u5c0f\u4e8e\u7b49\u4e8e\u5176\u7236\u4eb2\u7684\u952e\u503c\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u8282\u70b9\u7684\u952e\u503c\u90fd\u5927\u4e8e\u7b49\u4e8e\u5176\u7236\u4eb2\u952e\u503c\u7684\u5806\u53eb\u505a\u5c0f\u6839\u5806\uff0c\u5426\u5219\u53eb\u505a\u5927\u6839\u5806\u3002<a href=\"https:\/\/oi-wiki.org\/lang\/csl\/container-adapter\/#_13\">STL \u4e2d\u7684&nbsp;<code>priority_queue<\/code><\/a>&nbsp;\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u5927\u6839\u5806\u3002<\/p>\n\n\n\n<p>\uff08\u5c0f\u6839\uff09\u5806\u4e3b\u8981\u652f\u6301\u7684\u64cd\u4f5c\u6709\uff1a\u63d2\u5165\u4e00\u4e2a\u6570\u3001\u67e5\u8be2\u6700\u5c0f\u503c\u3001\u5220\u9664\u6700\u5c0f\u503c\u3001\u5408\u5e76\u4e24\u4e2a\u5806\u3001\u51cf\u5c0f\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u4e00\u4e9b\u529f\u80fd\u5f3a\u5927\u7684\u5806\uff08\u53ef\u5e76\u5806\uff09\u8fd8\u80fd\uff08\u9ad8\u6548\u5730\uff09\u652f\u6301 merge \u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u4e00\u4e9b\u529f\u80fd\u66f4\u5f3a\u5927\u7684\u5806\u8fd8\u652f\u6301\u53ef\u6301\u4e45\u5316\uff0c\u4e5f\u5c31\u662f\u5bf9\u4efb\u610f\u5386\u53f2\u7248\u672c\u8fdb\u884c\u67e5\u8be2\u6216\u8005\u64cd\u4f5c\uff0c\u4ea7\u751f\u65b0\u7684\u7248\u672c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"788\" height=\"599\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-42.png\" alt=\"\" class=\"wp-image-2855\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-42.png 788w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-42-300x228.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-42-768x584.png 768w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/figure>\n\n\n\n<h1>\u4e8c\u53c9\u5806<\/h1>\n\n\n\n<h3 id=\"_1\">\u7ed3\u6784<a href=\"https:\/\/oi-wiki.org\/ds\/binary-heap\/#_1\">\u00b6<\/a><\/h3>\n\n\n\n<p>\u4ece\u4e8c\u53c9\u5806\u7684\u7ed3\u6784\u8bf4\u8d77\uff0c\u5b83\u662f\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u5e76\u4e14\u662f\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u4e2d\u5b58\u6709\u4e00\u4e2a\u5143\u7d20\uff08\u6216\u8005\u8bf4\uff0c\u6709\u4e2a\u6743\u503c\uff09\u3002<\/p>\n\n\n\n<p>\u5806\u6027\u8d28\uff1a\u7236\u4eb2\u7684\u6743\u503c\u4e0d\u5c0f\u4e8e\u513f\u5b50\u7684\u6743\u503c\uff08\u5927\u6839\u5806\uff09\u3002\u540c\u6837\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5c0f\u6839\u5806\u3002\u672c\u6587\u4ee5\u5927\u6839\u5806\u4e3a\u4f8b\u3002<\/p>\n\n\n\n<p>\u7531\u5806\u6027\u8d28\uff0c\u6811\u6839\u5b58\u7684\u662f\u6700\u5927\u503c\uff08getmax \u64cd\u4f5c\u5c31\u89e3\u51b3\u4e86\uff09\u3002<\/p>\n\n\n\n<h3 id=\"_2\">\u63d2\u5165\u64cd\u4f5c<a href=\"https:\/\/oi-wiki.org\/ds\/binary-heap\/#_2\">\u00b6<\/a><\/h3>\n\n\n\n<p>\u63d2\u5165\u64cd\u4f5c\u662f\u6307\u5411\u4e8c\u53c9\u5806\u4e2d\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u8981\u4fdd\u8bc1\u63d2\u5165\u540e\u4e5f\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\u3002<\/p>\n\n\n\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\uff0c\u6700\u4e0b\u4e00\u5c42\u6700\u53f3\u8fb9\u7684\u53f6\u5b50\u4e4b\u540e\u63d2\u5165\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6700\u4e0b\u4e00\u5c42\u5df2\u6ee1\uff0c\u5c31\u65b0\u589e\u4e00\u5c42\u3002<\/p>\n\n\n\n<p>\u63d2\u5165\u4e4b\u540e\u53ef\u80fd\u4f1a\u4e0d\u6ee1\u8db3\u5806\u6027\u8d28\uff1f<\/p>\n\n\n\n<p><strong>\u5411\u4e0a\u8c03\u6574<\/strong>\uff1a\u5982\u679c\u8fd9\u4e2a\u7ed3\u70b9\u7684\u6743\u503c\u5927\u4e8e\u5b83\u7236\u4eb2\u7684\u6743\u503c\uff0c\u5c31\u4ea4\u6362\uff0c\u91cd\u590d\u6b64\u8fc7\u7a0b\u76f4\u5230\u4e0d\u6ee1\u8db3\u6216\u8005\u5230\u6839\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u8bc1\u660e\uff0c\u63d2\u5165\u4e4b\u540e\u5411\u4e0a\u8c03\u6574\u540e\uff0c\u6ca1\u6709\u5176\u4ed6\u7ed3\u70b9\u4f1a\u4e0d\u6ee1\u8db3\u5806\u6027\u8d28\u3002<\/p>\n\n\n\n<p>\u5411\u4e0a\u8c03\u6574\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f&nbsp;&nbsp;\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/oi-wiki.org\/ds\/images\/binary_heap_insert.svg\" alt=\"\u4e8c\u53c9\u5806\u7684\u63d2\u5165\u64cd\u4f5c\"\/><\/figure>\n\n\n\n<h3 id=\"_3\">\u5220\u9664\u64cd\u4f5c<a href=\"https:\/\/oi-wiki.org\/ds\/binary-heap\/#_3\">\u00b6<\/a><\/h3>\n\n\n\n<p>\u5220\u9664\u64cd\u4f5c\u6307\u5220\u9664\u5806\u4e2d\u6700\u5927\u7684\u5143\u7d20\uff0c\u5373\u5220\u9664\u6839\u7ed3\u70b9\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u5982\u679c\u76f4\u63a5\u5220\u9664\uff0c\u5219\u53d8\u6210\u4e86\u4e24\u4e2a\u5806\uff0c\u96be\u4ee5\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u4e0d\u59a8\u8003\u8651\u63d2\u5165\u64cd\u4f5c\u7684\u9006\u8fc7\u7a0b\uff0c\u8bbe\u6cd5\u5c06\u6839\u7ed3\u70b9\u79fb\u5230\u6700\u540e\u4e00\u4e2a\u7ed3\u70b9\uff0c\u7136\u540e\u76f4\u63a5\u5220\u6389\u3002<\/p>\n\n\n\n<p>\u7136\u800c\u5b9e\u9645\u4e0a\u4e0d\u597d\u505a\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u7684\u65b9\u6cd5\u662f\uff0c\u628a\u6839\u7ed3\u70b9\u548c\u6700\u540e\u4e00\u4e2a\u7ed3\u70b9\u76f4\u63a5\u4ea4\u6362\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\u76f4\u63a5\u5220\u6389\uff08\u5728\u6700\u540e\u4e00\u4e2a\u7ed3\u70b9\u5904\u7684\uff09\u6839\u7ed3\u70b9\uff0c\u4f46\u662f\u65b0\u7684\u6839\u7ed3\u70b9\u53ef\u80fd\u4e0d\u6ee1\u8db3\u5806\u6027\u8d28\u2026\u2026<\/p>\n\n\n\n<p><strong>\u5411\u4e0b\u8c03\u6574<\/strong>\uff1a\u5728\u8be5\u7ed3\u70b9\u7684\u513f\u5b50\u4e2d\uff0c\u627e\u4e00\u4e2a\u6700\u5927\u7684\uff0c\u4e0e\u8be5\u7ed3\u70b9\u4ea4\u6362\uff0c\u91cd\u590d\u6b64\u8fc7\u7a0b\u76f4\u5230\u5e95\u5c42\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u8bc1\u660e\uff0c\u5220\u9664\u5e76\u5411\u4e0b\u8c03\u6574\u540e\uff0c\u6ca1\u6709\u5176\u4ed6\u7ed3\u70b9\u4e0d\u6ee1\u8db3\u5806\u6027\u8d28\u3002<\/p>\n\n\n\n<h3 id=\"_4\">\u51cf\u5c0f\u67d0\u4e2a\u70b9\u7684\u6743\u503c<a href=\"https:\/\/oi-wiki.org\/ds\/binary-heap\/#_4\">\u00b6<\/a><\/h3>\n\n\n\n<p>\u5f88\u663e\u7136\uff0c\u76f4\u63a5\u4fee\u6539\u540e\uff0c\u5411\u4e0a\u8c03\u6574\u4e00\u6b21\u5373\u53ef<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"658\" height=\"671\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-43.png\" alt=\"\" class=\"wp-image-2856\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-43.png 658w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-43-294x300.png 294w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\u53c2\u8003\u4ee3\u7801\uff1a\r\n\r\n\r\nvoid up(int x) {\r\n  while (x > 1 &amp;&amp; h&#91;x] > h&#91;x \/ 2]) {\r\n    swap(h&#91;x], h&#91;x \/ 2]);\r\n    x \/= 2;\r\n  }\r\n}\r\n\r\nvoid down(int x) {\r\n  while (x * 2 &lt;= n) {\r\n    t = x * 2;\r\n    if (t + 1 &lt;= n &amp;&amp; h&#91;t + 1] > h&#91;t]) t++;\r\n    if (h&#91;t] &lt;= h&#91;x]) break;\r\n    std::swap(h&#91;x], h&#91;t]);\r\n    x = t;\r\n  }\r\n}<\/code><\/pre>\n\n\n\n<p>\u5efa\u5806<a href=\"https:\/\/oi-wiki.org\/ds\/binary-heap\/#_6\">\u00b6<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 decreasekey\uff08\u5373\uff0c\u5411\u4e0a\u8c03\u6574\uff09\u00b6\r\n\u4ece\u6839\u5f00\u59cb\uff0c\u6309 BFS \u5e8f\u8fdb\u884c\u3002\r\n\r\n\r\nvoid build_heap_1() {\r\n  for (i = 1; i &lt;= n; i++) up(i);\r\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528\u5411\u4e0b\u8c03\u6574\u00b6\r\n\u8fd9\u65f6\u6362\u4e00\u79cd\u601d\u8def\uff0c\u4ece\u53f6\u5b50\u5f00\u59cb\uff0c\u9010\u4e2a\u5411\u4e0b\u8c03\u6574\r\n\r\n\r\nvoid build_heap_2() {\r\n  for (i = n; i >= 1; i--) down(i);\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python\u4e2d\u76f4\u63a5\u4f7f\u7528heapq\u5efa\u7acb \u5c0f\u6839\u5806 http:\/\/139.9.1.231\/index.php\/202 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/10\/heapq_1\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u6570\u636e\u7ed3\u6784 &#8212; \u5806<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2853"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2853"}],"version-history":[{"count":11,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2853\/revisions"}],"predecessor-version":[{"id":2868,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2853\/revisions\/2868"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2853"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}