{"id":484,"date":"2023-04-25T13:25:01","date_gmt":"2023-04-25T05:25:01","guid":{"rendered":"https:\/\/blog.cirno.fun\/?p=484"},"modified":"2025-03-19T00:33:57","modified_gmt":"2025-03-18T16:33:57","slug":"%e5%90%88%e5%b9%b6%e6%9e%9c%e5%ad%90huffman%e6%a0%91%e6%a8%a1%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/blog.sssn.tech\/?p=484","title":{"rendered":"\u5408\u5e76\u679c\u5b50(Huffman\u6811\u6a21\u677f)"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/blog.sssn.tech\/wp-content\/uploads\/2023\/04\/image-1682391289011.png\" alt=\"file\" \/><img decoding=\"async\" src=\"https:\/\/blog.sssn.tech\/wp-content\/uploads\/2023\/04\/image-1682391319738.png\" alt=\"file\" \/><\/p>\n<p>\u5408\u5e76\u679c\u5b50\u95ee\u9898\u53ef\u4ee5\u62bd\u8c61\u6210\u5982\u4e0b\u6a21\u578b\uff1a \u5bf9\u4e8e\u4e00\u4e2a\u6570\u96c6s\uff0c\u6c42\u4e00\u4e2a\u5b8c\u5168\u4e8c\u53c9\u6811\u6811\uff0c\u4ee5\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4f5c\u4e3a\u53f6\u8282\u70b9\uff0c\u4f7f\u5f97\u8be5\u6811\u53f6\u8282\u70b9\u7684<strong>\u5e26\u6743\u8def\u5f84\u548c\u6700\u5c0f<\/strong>\uff0c\u8fd9\u79cd\u6811\u5c31\u662fHuffman\u6811<\/p>\n<p>\u5e26\u6743\u8def\u5f84\u548c\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b<\/p>\n<pre><code class=\"language-katex\">\\sum_{i=1}^{n} w_id_i<\/code><\/pre>\n<p>\u5176\u4e2dwi\u662f\u53f6\u8282\u70b9\u7684\u6743\u503c\uff0cdi\u662f\u6df1\u5ea6<\/p>\n<p>\u4f8b\u5982\u4e0b\u56fe\u662f\u4e00\u9897Huffman\u6811 <img decoding=\"async\" src=\"https:\/\/blog.sssn.tech\/wp-content\/uploads\/2023\/04\/image-1682397966905.png\" alt=\"file\" \/>\u5176\u53f6\u8282\u70b9\u67095\u4e2a\uff1a1,2,3,4,5\uff0c\u4e14\u603b\u6743\u503c\u4e3a33<\/p>\n<p>\u4e0d\u96be\u770b\u51faHuffman\u6811\u7684\u6784\u9020\u662f\u4e00\u4e2a\u8d2a\u5fc3\u95ee\u9898\uff0c\u56e0\u4e3a\u4f4d\u7f6e\u8d8a\u6df1\u7684\u8282\u70b9\u9700\u8981\u4e58\u4ee5\u8d8a\u5927\u7684\u6df1\u5ea6d\uff0c\u6240\u4ee5\u6211\u4eec\u5e0c\u671b\u4ed6\u7684\u6743\u503c\u5c3d\u91cf\u7684\u5c0f<\/p>\n<p><strong>\u6743\u503c\u8d8a\u5c0f\u7684\u70b9\u8d8a\u9760\u4e0b<\/strong>\u662fHuffman\u6811\u7684\u6838\u5fc3\u601d\u60f3<\/p>\n<p>\u5408\u5e76\u679c\u5b50\u8fd9\u9898\u662fHuffman\u6570\u7684\u6a21\u677f\uff0cHuffman\u6811\u7684\u5b9e\u73b0\u53ef\u4ee5\u76f4\u63a5\u7528\u5c0f\u6839\u5806\uff0c\u6bcf\u6b21\u5f39\u51fa\u4e24\u4e2a\u5806\u9876\u5143\u7d20\uff0c\u5408\u5e76\u540e\u518d\u63d2\u5165\u5806\u4e2d\u5373\u53ef<\/p>\n<pre><code class=\"language-cpp\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\n\nint main() {\n    int n;\n    cin &gt;&gt; n;\n    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; Q;\n\n    for (int i = 1; i &lt;= n; i++) {\n        int x;\n        cin &gt;&gt; x;\n        Q.push(x);\n    }\n\n    int res = 0;\n    while(Q.size() != 1) {\n        int a = Q.top();\n        Q.pop();\n        int b = Q.top();\n        Q.pop();\n        Q.push(a + b);\n        res += a + b;\n    }\n\n    cout &lt;&lt; res &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5408\u5e76\u679c\u5b50\u95ee\u9898\u53ef\u4ee5\u62bd\u8c61\u6210\u5982\u4e0b\u6a21\u578b\uff1a \u5bf9\u4e8e\u4e00\u4e2a\u6570\u96c6s\uff0c\u6c42\u4e00\u4e2a\u5b8c\u5168\u4e8c\u53c9\u6811\u6811\uff0c\u4ee5\u96c6\u5408\u4e2d\u7684 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21],"tags":[],"class_list":["post-484","post","type-post","status-publish","format-standard","hentry","category-huffman","category-21"],"_links":{"self":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=484"}],"version-history":[{"count":2,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/484\/revisions"}],"predecessor-version":[{"id":838,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/484\/revisions\/838"}],"wp:attachment":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}