{"id":194,"date":"2025-07-22T19:53:36","date_gmt":"2025-07-22T11:53:36","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=194"},"modified":"2025-11-24T16:40:42","modified_gmt":"2025-11-24T08:40:42","slug":"%e5%a0%86%ef%bc%882%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=194","title":{"rendered":"\u5806\uff082\uff09"},"content":{"rendered":"\n<p>\u5efa\u8bae\u9605\u8bfb\u672c\u6587\u524d\u5148\u9605\u8bfb\u4e0a\u7bc7\u6587\u7ae0<a href=\"http:\/\/www.tgwttt.xyz\/?p=187\">http:\/\/www.tgwttt.xyz\/?p=187<\/a><\/p>\n\n\n\n<p>\u63a5\u4e0a\u6587\uff0c\u672c\u6587\u4e3b\u8981\u8c08\u8bba\u5806\u6392\u5e8f\u548cTop_k\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u4e00.\u5806\u6392\u5e8f\uff1a<\/strong><\/p>\n\n\n\n<p>\u5806\u6392\u5e8f\u5efa\u5806\u5171\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u5411\u4e0a\u8c03\u6574\u5efa\u5806\u548c\u5411\u4e0b\u8c03\u6574\u5efa\u5806\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u5206\u522b\u6765\u5206\u6790\u4e00\u4e0b\u4e24\u8005\u7684\u4f18\u52a3\uff1a<\/p>\n\n\n\n<p>\u5411\u4e0a\u8c03\u6574\u5efa\u5806\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nvoid AdjustUp(HPDataType* a, int child)\n{\n\t\/\/ \u521d\u59cb\u6761\u4ef6\n\t\/\/ \u4e2d\u95f4\u8fc7\u7a0b\n\t\/\/ \u7ed3\u675f\u6761\u4ef6\n\tint parent = (child - 1) \/ 2;\n\t\/\/while (parent >= 0)\n\twhile (child > 0)\n\t{\n\t\tif (a[child] < a[parent])\n\t\t{\n\t\t\tSwap(&#038;a[child], &#038;a[parent]);\n\t\t\tchild = parent;\n\t\t\tparent = (child - 1) \/ 2;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t}\n}\nfor (int i = 1; i < n; i++)\n{\n\tAdjustUp(a, i);\n}\nint end = n - 1;\nwhile (end > 0)\n{\n\tSwap(&a[0], &a[end]);\n\tAdjustDown(a, end, 0);\n\t--end;\n}<\/code><\/pre>\n\n\n\n<p>\u5411\u4e0a\u8c03\u6574\u5efa\u5806\uff1a\u662f\u5229\u7528\u5806\u7684\u63d2\u5165\uff0c\u7136\u540e\u518d\u5411\u4e0a\u8c03\u6574\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u8ba1\u7b97\uff1a<\/p>\n\n\n\n<p><em>T<\/em>(<em>n<\/em>)=<em>k<\/em>=0\u2211<em>h<\/em>\u22122\u200b2<em>k<\/em>\u22c5(<em>h<\/em>\u22121\u2212<em>k<\/em>)<\/p>\n\n\n\n<p><em>T<\/em>(<em>n<\/em>)=20\u22c5(<em>h<\/em>\u22121)+21\u22c5(<em>h<\/em>\u22122)+22\u22c5(<em>h<\/em>\u22123)+\u22ef+2<em>h<\/em>\u22122\u22c51<\/p>\n\n\n\n<p><strong class=\"\">\u00a0<\/strong>\u518d\u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff1a<\/p>\n\n\n\n<p><em>T<\/em>(<em>n<\/em>)\u200b=[2(<em>h<\/em>\u22121)\u2212(<em>h<\/em>\u22121)]+[4(<em>h<\/em>\u22122)\u22122(<em>h<\/em>\u22122)]+\u22ef+[2<em>h<\/em>\u22121\u22c51\u22122<em>h<\/em>\u22122\u22c51]\u22122<em>h<\/em>\u22121\u22c50=<\/p>\n\n\n\n<p>(<em>h<\/em>\u22121)+2(<em>h<\/em>\u22122)\u22122(<em>h<\/em>\u22122)+4(<em>h<\/em>\u22123)\u22124(<em>h<\/em>\u22123)+\u22ef+2<em>h<\/em>\u22122\u22c51\u22122<em>h<\/em>\u22122\u22c51+2<em>h<\/em>\u22121\u22c51=<\/p>\n\n\n\n<p><em>k<\/em>=0\u2211<em>h<\/em>\u22122\u200b2<em>k<\/em>\u2212(<em>h<\/em>\u22121)\u22c520+2<em>h<\/em>\u22121\u22c51=(2<em>h<\/em>\u22121\u22121)\u2212(<em>h<\/em>\u22121)+2<em>h<\/em>\u22121=<\/p>\n\n\n\n<p>-N+(N+1)log(N+1)-1<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aN*log(2)N<\/p>\n\n\n\n<p>\u5411\u4e0b\u8c03\u6574\u5efa\u5806\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void AdjustDown(HPDataType* a, int n, int parent)\n{\n\t\/\/ \u5148\u5047\u8bbe\u5de6\u5b69\u5b50\u5c0f\n\tint child = parent * 2 + 1;\n\twhile (child < n)  \/\/ child >= n\u8bf4\u660e\u5b69\u5b50\u4e0d\u5b58\u5728\uff0c\u8c03\u6574\u5230\u53f6\u5b50\u4e86\n\t{\n\t\t\/\/ \u627e\u51fa\u5c0f\u7684\u90a3\u4e2a\u5b69\u5b50\n\t\tif (child + 1 < n &#038;&#038; a[child + 1] < a[child])\n\t\t{\n\t\t\t++child;\n\t\t}\n\n\t\tif (a[child] < a[parent])\n\t\t{\n\t\t\tSwap(&#038;a[child], &#038;a[parent]);\n\t\t\tparent = child;\n\t\t\tchild = parent * 2 + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t}\n}\nfor (int i = (n - 1 - 1) \/ 2; i >= 0; i--)\n{\n\tAdjustDown(a, n, i);\n}\nint end = n - 1;\nwhile (end > 0)\n{\n\tSwap(&a[0], &a[end]);\n\tAdjustDown(a, end, 0);\n\t--end;\n}\n<\/code><\/pre>\n\n\n\n<p>\u4e0e\u5411\u4e0a\u8c03\u6574\u5efa\u5806\u76f8\u6bd4\uff1a\u5411\u4e0b\u8c03\u6574\u5efa\u5806\u5219\u662f\u4ece(n-2)\/2\uff0c\u5f00\u59cb\u8c03\u6574\u5806\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"805\" height=\"206\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-38.png\" alt=\"\" class=\"wp-image-195\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-38.png 805w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-38-300x77.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-38-768x197.png 768w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/><\/figure>\n\n\n\n<p><br>\u65f6\u95f4\u590d\u6742\u5ea6\u8ba1\u7b97\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"929\" height=\"325\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-39.png\" alt=\"\" class=\"wp-image-196\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-39.png 929w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-39-300x105.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-39-768x269.png 768w\" sizes=\"auto, (max-width: 929px) 100vw, 929px\" \/><\/figure>\n\n\n\n<p>\u53ef\u77e5\u5411\u4e0a\u8c03\u6574\u5efa\u5806\u52a3\u4e8e\u5411\u4e0b\u8c03\u6574\u5efa\u5806<\/p>\n\n\n\n<p><strong>\u4e8c.TOP_K\u95ee\u9898<\/strong><\/p>\n\n\n\n<p>top_K\u95ee\u9898\u662f\u4eceN\u4e2a\u6570\u4e2d\u5feb\u901f\u627e\u5230\u524dK\u4e2a\u5927\u7684\u6570\uff08N>>k\uff09<\/p>\n\n\n\n<p>\u6bd4\u5982\uff1a\u4e13\u4e1a\u524d10\u540d\u3001\u4e16\u754c500\u5f3a\u3001\u5bcc\u8c6a\u699c\u3001\u6e38\u620f\u4e2d\u524d100\u7684\u6d3b\u8dc3\u73a9\u5bb6\u7b49\u3002\u5bf9\u4e8eTop-K\u95ee\u9898\uff0c\u80fd\u60f3\u5230\u7684\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\u5c31\u662f\u6392\u5e8f\uff0c\u4f46\u662f\uff1a\u5982\u679c\u6570\u636e\u91cf\u975e\u5e38\u5927\uff0c\u6392\u5e8f\u5c31\u4e0d\u592a\u53ef\u53d6\u4e86(\u53ef\u80fd\u6570\u636e\u90fd\u4e0d\u80fd\u4e00\u4e0b\u5b50\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d)\u3002\u6700\u4f73\u7684\u65b9\u5f0f\u5c31\u662f\u7528\u5806\u6765\u89e3\u51b3\uff0c<\/p>\n\n\n\n<p>\u57fa\u672c\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1. \u7528\u6570\u636e\u96c6\u5408\u4e2d\u524dK\u4e2a\u5143\u7d20\u6765\u5efa\u5806\u524dk\u4e2a\u6700\u5927\u7684\u5143\u7d20\uff0c\u5219\u5efa\u5c0f\u5806\u524dk\u4e2a\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5219\u5efa\u5927\u5806<\/p>\n\n\n\n<p>2. \u7528\u5269\u4f59\u7684N-K\u4e2a\u5143\u7d20\u4f9d\u6b21\u4e0e\u5806\u9876\u5143\u7d20\u6765\u6bd4\u8f83\uff0c\u4e0d\u6ee1\u8db3\u5219\u66ff\u6362\u5806\u9876\u5143\u7d20\u5c06\u5269\u4f59N-K\u4e2a\u5143\u7d20\u4f9d\u6b21\u4e0e\u5806\u9876\u5143\u7d20\u6bd4\u5b8c\u4e4b\u540e\uff0c\u5806\u4e2d\u5269\u4f59\u7684K\u4e2a\u5143\u7d20\u5c31\u662f\u6240\u6c42\u7684\u524dK\u4e2a\u6700\u5c0f\u6216\u8005\u6700\u5927\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u4e0a\u8ff0\u95ee\u9898\u4f1a\u51fa\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c\u6570\u636e\u91cf\u592a\u5927\u30021e10\u5462\uff1f\u6b64\u65f6\u6211\u4eec\u5c31\u9700\u89814G,\u7a7a\u95f4\u5f00\u9500\u8fc7\u5927\uff0c\u5982\u679c\u53ea\u7ed9\u4f604M,\u4f60\u4f1a\u600e\u4e48\u505a\u5462\uff1f<\/p>\n\n\n\n<p>\u7b54\u6848\u662f\uff1a\u5148\u5efa\u4e00\u4e2aK\u4e2a\u6570\u636e\u5927\u5c0f\u7684\u5c0f\u5806\uff0c\u518d\u8ba9\u5176\u4ed6\u5269\u4e0b\u7684\u6570\u4e0e\u4e4b\u5806\u9876\u6bd4\u8f83\uff0c\u82e5\u5927\u4e8e\u6808\u9876\u7684\u5143\u7d20\uff0c\u5219\u5165\u5806\uff0c\u6362\u6389\u6808\u9876\u7684\u5143\u7d20\uff0c\u5411\u4e0b\u8c03\u6574\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\uff0c\u5c31\u5230\u8fd9\u91cc\u5982\u6709\u9519\u8bef\u6b22\u8fce\u6307\u51fa\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u8bae\u9605\u8bfb\u672c\u6587\u524d\u5148\u9605\u8bfb\u4e0a\u7bc7\u6587\u7ae0http:\/\/www.tgwttt.xyz\/?p=187 \u63a5\u4e0a\u6587\uff0c\u672c\u6587\u4e3b\u8981\u8c08\u8bba\u5806\u6392&#8230;<\/p>\n","protected":false},"author":1,"featured_media":195,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":867,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/194\/revisions\/867"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/195"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}