{"id":64,"date":"2019-04-11T03:13:24","date_gmt":"2019-04-10T19:13:24","guid":{"rendered":"http:\/\/120.55.184.7\/?p=64"},"modified":"2025-04-15T18:17:55","modified_gmt":"2025-04-15T10:17:55","slug":"%e4%ba%8c%e5%8f%89%e5%a0%86","status":"publish","type":"post","link":"https:\/\/beijian99.top\/?p=64","title":{"rendered":"\u4e8c\u53c9\u5806"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u4e8c\u53c9\u5806\u7684\u5b9a\u4e49\u4e0e\u7279\u6027<\/h3>\n\n\n\n<p>\u4e8c\u53c9\u5806\u662f\u4e00\u79cd\u57fa\u4e8e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5206\u4e3a\u200b<strong>\u200b\u6700\u5927\u5806\u200b<\/strong>\u200b\u548c\u200b<strong>\u200b\u6700\u5c0f\u5806\u200b<\/strong>\u200b\u4e24\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u6700\u5927\u5806\u200b<\/strong>\u200b\uff1a\u4efb\u610f\u7236\u8282\u70b9\u7684\u952e\u503c \u2265 \u5b50\u8282\u70b9\uff1b<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u6700\u5c0f\u5806\u200b<\/strong>\u200b\uff1a\u4efb\u610f\u7236\u8282\u70b9\u7684\u952e\u503c \u2264 \u5b50\u8282\u70b9\uff1b<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u5b8c\u5168\u4e8c\u53c9\u6811\u7279\u6027\u200b<\/strong>\u200b\uff1a\u9664\u6700\u540e\u4e00\u5c42\u5916\uff0c\u5176\u4ed6\u5c42\u8282\u70b9\u5168\u6ee1\uff0c\u6700\u540e\u4e00\u5c42\u8282\u70b9\u5de6\u5bf9\u9f50\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u5b58\u50a8\u7ed3\u6784\u4e0e\u7d22\u5f15\u8ba1\u7b97<\/h3>\n\n\n\n<p>\u4e8c\u53c9\u5806\u901a\u5e38\u7528\u200b<strong>\u200b\u6570\u7ec4\u200b<\/strong>\u200b\u5b9e\u73b0\uff0c\u6839\u636e\u6839\u8282\u70b9\u4e0b\u6807\u4e0d\u540c\u5206\u4e3a\u4e24\u79cd\u5b58\u50a8\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u6839\u8282\u70b9\u4e0b\u6807\u4e3a1\u200b<\/strong>\u200b\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5b50\u8282\u70b9\u4f4d\u7f6e\uff1a<code>2n<\/code>\uff08\u5de6\uff09\u3001<code>2n+1<\/code>\uff08\u53f3\uff09<\/li>\n\n\n\n<li>\u7236\u8282\u70b9\u4f4d\u7f6e\uff1a<code>n\/\/2<\/code>\uff08\u5411\u4e0b\u53d6\u6574\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u6839\u8282\u70b9\u4e0b\u6807\u4e3a0\u200b<\/strong>\u200b\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5b50\u8282\u70b9\u4f4d\u7f6e\uff1a<code>2n+1<\/code>\uff08\u5de6\uff09\u3001<code>2n+2<\/code>\uff08\u53f3\uff09<\/li>\n\n\n\n<li>\u7236\u8282\u70b9\u4f4d\u7f6e\uff1a<code>\u230a(n-1)\/2\u230b<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u200b<strong>\u200b\u793a\u4f8b\u200b<\/strong>\u200b\uff1a\u6839\u8282\u70b9\u4e3a1\u7684\u5806\u6570\u7ec4<code>[_,1,2,3,4,5,6,7]<\/code>\uff08\u9996\u5143\u7d20\u5360\u4f4d\uff09\uff0c\u8282\u70b93\u7684\u5b50\u8282\u70b9\u4e3a6\u548c7<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u6838\u5fc3\u64cd\u4f5c\u4e0e\u7b97\u6cd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u63d2\u5165\u64cd\u4f5c\uff08\u4e0a\u6d6e\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u6b65\u9aa4\u200b<\/strong>\u200b\uff1a\u63d2\u5165\u5230\u6570\u7ec4\u672b\u5c3e\uff0c\u9010\u5c42\u4e0e\u7236\u8282\u70b9\u6bd4\u8f83\u5e76\u4ea4\u6362\uff0c\u76f4\u5230\u6ee1\u8db3\u5806\u6027\u8d28\uff1b<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u65f6\u95f4\u590d\u6742\u5ea6\u200b<\/strong>\u200b\uff1a O(log <sup>n<\/sup>) <\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u5220\u9664\u5806\u9876\uff08\u4e0b\u6c89\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u6b65\u9aa4\u200b<\/strong>\u200b\uff1a\n<ol class=\"wp-block-list\">\n<li>\u7528\u672b\u5c3e\u5143\u7d20\u66ff\u6362\u5806\u9876\uff1b<\/li>\n\n\n\n<li>\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4e0e\u8f83\u5927\uff08\u6700\u5927\u5806\uff09\/\u8f83\u5c0f\uff08\u6700\u5c0f\u5806\uff09\u5b50\u8282\u70b9\u4ea4\u6362\uff0c\u76f4\u5230\u6ee1\u8db3\u5806\u6027\u8d28\uff1b<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u65f6\u95f4\u590d\u6742\u5ea6\u200b<\/strong>\u200b\uff1aO(log <sup>n<\/sup>) <\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u5efa\u5806\u7b97\u6cd5<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u81ea\u9876\u5411\u4e0b\u6cd5\u200b<\/strong>\u200b\uff1a\u9010\u4e2a\u63d2\u5165\u5143\u7d20\uff0c\u603b\u65f6\u95f4\u590d\u6742\u5ea6O(n log n)\uff1b<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u81ea\u5e95\u5411\u4e0a\u6cd5\u200b<\/strong>\u200b\uff1a\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u5f00\u59cb\u8c03\u6574\uff08Floyd\u7b97\u6cd5\uff09\uff0c\u65f6\u95f4\u590d\u6742\u5ea6O(n)\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u6027\u80fd\u4e0e\u5e94\u7528\u573a\u666f<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u65f6\u95f4\u590d\u6742\u5ea6\u200b<\/strong>\u200b\uff1a\n<ul class=\"wp-block-list\">\n<li>\u67e5\u8be2\u6781\u503c\uff1aO(1)\uff1b<\/li>\n\n\n\n<li>\u63d2\u5165\/\u5220\u9664\uff1aO(log <sup>n<\/sup>)\uff1b<\/li>\n\n\n\n<li>\u5408\u5e76\u5806\uff1aO(n+k)\uff08\u6570\u7ec4\u62fc\u63a5\u540e\u91cd\u5efa\u5806\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u5178\u578b\u5e94\u7528\u200b<\/strong>\u200b\uff1a\n<ul class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u5806\u6392\u5e8f\u200b<\/strong>\u200b\uff1a\u901a\u8fc7\u53cd\u590d\u53d6\u51fa\u5806\u9876\u5b9e\u73b0\u6392\u5e8f\uff1b<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u4f18\u5148\u961f\u5217\u200b<\/strong>\u200b\uff1a\u4efb\u52a1\u8c03\u5ea6\u3001Dijkstra\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff1b<\/li>\n\n\n\n<li>\u200b<strong>\u200bTop K\u95ee\u9898\u200b<\/strong>\u200b\uff1a\u7ef4\u62a4\u5927\u5c0f\u4e3aK\u7684\u6700\u5c0f\u5806\u5feb\u901f\u7b5b\u9009\u6700\u5927\u5143\u7d20\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>\u4ee3\u7801\u5b9e\u73b0<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; gutter: true; title: ; notranslate\" title=\"\">\n#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\n\nclass PriorityQueue {\nprivate:\n    vector&lt;int&gt; a; \/\/ \u4f7f\u7528vector\u6765\u5b58\u50a8\u5806\u7684\u5143\u7d20\n\n    \/\/ \u4e0a\u6d6e\u64cd\u4f5c\uff0c\u7528\u4e8e\u5728\u63d2\u5165\u65b0\u5143\u7d20\u540e\u6062\u590d\u5806\u7684\u6027\u8d28\n    void up() {\n        int n = a.size(); \/\/ \u83b7\u53d6\u5806\u7684\u5927\u5c0f\n        int i = n - 1; \/\/ \u65b0\u63d2\u5165\u5143\u7d20\u7684\u4f4d\u7f6e\n        \/\/ \u5f53\u5143\u7d20\u4e0d\u662f\u6839\u8282\u70b9\u4e14\u6bd4\u7236\u8282\u70b9\u5c0f\u7684\u65f6\u5019\uff0c\u6267\u884c\u4e0a\u6d6e\u64cd\u4f5c\n        while (i &gt; 0 &amp;&amp; a&#x5B;i\/2] &gt; a&#x5B;i]) {\n            swap(a&#x5B;i\/2], a&#x5B;i]); \/\/ \u4ea4\u6362\u5f53\u524d\u8282\u70b9\u548c\u7236\u8282\u70b9\u7684\u503c\n            i \/= 2; \/\/ \u66f4\u65b0\u5f53\u524d\u8282\u70b9\u7d22\u5f15\u4e3a\u5176\u7236\u8282\u70b9\u7684\u7d22\u5f15\n        }\n    }\n\n    \/\/ \u4e0b\u6c89\u64cd\u4f5c\uff0c\u7528\u4e8e\u5728\u5220\u9664\u5806\u9876\u5143\u7d20\u540e\u6062\u590d\u5806\u7684\u6027\u8d28\n    void down() {\n        int n = a.size(); \/\/ \u83b7\u53d6\u5806\u7684\u5927\u5c0f\n        int i = n - 1; \/\/ \u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\n        a&#x5B;0] = a&#x5B;i]; \/\/ \u5c06\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u79fb\u52a8\u5230\u5806\u9876\n        a.pop_back(); \/\/ \u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n        i--; \/\/ \u66f4\u65b0\u5806\u7684\u5927\u5c0f\n        int cur = 0; \/\/ \u5f53\u524d\u8282\u70b9\u4f4d\u7f6e\n        int nxt; \/\/ \u4e0b\u4e00\u4e2a\u8282\u70b9\u4f4d\u7f6e\n        \/\/ \u5f53\u5f53\u524d\u8282\u70b9\u6709\u5b50\u8282\u70b9\u65f6\uff0c\u6267\u884c\u4e0b\u6c89\u64cd\u4f5c\n        while (cur &lt;= i) {\n            nxt = cur &lt;&lt; 1; \/\/ \u8ba1\u7b97\u5de6\u5b50\u8282\u70b9\u7684\u4f4d\u7f6e\n            \/\/ \u5982\u679c\u53f3\u5b50\u8282\u70b9\u5b58\u5728\u4e14\u5c0f\u4e8e\u5de6\u5b50\u8282\u70b9\uff0c\u9009\u62e9\u53f3\u5b50\u8282\u70b9\n            if (cur &lt;&lt; 1 | 1 &lt;= i &amp;&amp; a&#x5B;cur &lt;&lt; 1] &gt; a&#x5B;cur &lt;&lt; 1 | 1]) {\n                nxt = cur &lt;&lt; 1 | 1;\n            }\n            \/\/ \u5982\u679c\u5f53\u524d\u8282\u70b9\u5c0f\u4e8e\u5176\u5b50\u8282\u70b9\uff0c\u5219\u4e0d\u9700\u8981\u7ee7\u7eed\u4e0b\u6c89\n            if (nxt &gt; i || a&#x5B;cur] &lt;= a&#x5B;nxt]) break;\n            swap(a&#x5B;cur], a&#x5B;nxt]); \/\/ \u4ea4\u6362\u5f53\u524d\u8282\u70b9\u548c\u6700\u5c0f\u5b50\u8282\u70b9\u7684\u503c\n            cur = nxt; \/\/ \u66f4\u65b0\u5f53\u524d\u8282\u70b9\u7d22\u5f15\u4e3a\u6700\u5c0f\u5b50\u8282\u70b9\u7684\u7d22\u5f15\n        }\n    }\npublic:\n    PriorityQueue() {\n        a.clear(); \/\/ \u6784\u9020\u51fd\u6570\uff0c\u521d\u59cb\u5316\u5806\n    }\n\n    \/\/ \u5411\u5806\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\n    void push(int v) {\n        a.push_back(v); \/\/ \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u7684\u672b\u5c3e\n        up(); \/\/ \u6267\u884c\u4e0a\u6d6e\u64cd\u4f5c\u4ee5\u6062\u590d\u5806\u7684\u6027\u8d28\n    }\n\n    \/\/ \u83b7\u53d6\u5806\u9876\u5143\u7d20\n    int top() {\n        return a&#x5B;0]; \/\/ \u5806\u9876\u5143\u7d20\u5373\u4e3a\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n    }\n\n    \/\/ \u5220\u9664\u5806\u9876\u5143\u7d20\n    void pop() {\n        down(); \/\/ \u6267\u884c\u4e0b\u6c89\u64cd\u4f5c\u4ee5\u6062\u590d\u5806\u7684\u6027\u8d28\n    }\n};\n\nint main() {\n    ios::sync_with_stdio(false); \/\/ \u5173\u95ed\u540c\u6b65\uff0c\u63d0\u9ad8cin\u548ccout\u7684\u6548\u7387\n    cin.tie(0); \/\/ \u89e3\u9664cin\u548ccout\u7684\u7ed1\u5b9a\n    cout.tie(0); \/\/ \u89e3\u9664cout\u7684\u7ed1\u5b9a\n    auto q = PriorityQueue(); \/\/ \u521b\u5efa\u4e00\u4e2a\u4f18\u5148\u961f\u5217\u5bf9\u8c61\n    int n;\n    cin &gt;&gt; n; \/\/ \u8bfb\u53d6\u64cd\u4f5c\u7684\u6570\u91cf\n    for (int i = 0; i &lt; n; i++) {\n        int op;\n        cin &gt;&gt; op; \/\/ \u8bfb\u53d6\u64cd\u4f5c\u7c7b\u578b\n        switch (op) {\n            case 1:\n                int x;\n                cin &gt;&gt; x; \/\/ \u8bfb\u53d6\u8981\u6dfb\u52a0\u7684\u5143\u7d20\n                q.push(x); \/\/ \u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u4f18\u5148\u961f\u5217\n                break;\n            case 2:\n                cout &lt;&lt; q.top() &lt;&lt; endl; \/\/ \u6253\u5370\u5806\u9876\u5143\u7d20\n                break;\n            case 3:\n                q.pop(); \/\/ \u5220\u9664\u5806\u9876\u5143\u7d20\n                break;\n        }\n    }\n    return 0;\n}\n\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4e8c\u53c9\u5806\u7684\u5b9a\u4e49\u4e0e\u7279\u6027 \u4e8c\u53c9\u5806\u662f\u4e00\u79cd\u57fa\u4e8e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5206\u4e3a\u200b\u200b\u6700\u5927\u5806\u200b\u200b\u548c\u200b\u200b\u6700\u5c0f\u5806\u200b\u200b\u4e24\u7c7b\uff1a \u4e8c\u3001\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":389,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[4],"tags":[119],"class_list":["post-64","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm","tag-119"],"_links":{"self":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":20,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":467,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions\/467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/media\/389"}],"wp:attachment":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}