[BZOJ4198] [Noi2015] Homer Epic (greedy)

Source: Internet
Author: User

DescriptionThe one who chases the shadow, himself is the shadow. --Homer Allison has recently been fascinated by literature. She likes to have a cappuccino on a lazy afternoon and read the story of Homer in her own way.  But the monumental work Homer Epic, made up of the Odyssey and the Iliad, is too long, and Allison wants to make it shorter by a coding method. There are n different words in Homer's epic, numbered from 1 to N. The total number of occurrences of the first word I was wi.  Allison wants to replace the word I with the K-string Si, making it meet the following requirements: For any 1≤i,j≤n,i≠j, there are: Si is not the prefix of SJ. Allison now wants to know how to choose Si to make the new Homer epic the least long.  Allison also wants to know the shortest length of the longest si in the case that the total length is minimal.  A string is called a K-binary string, when and only if its characters are 0 to k−1 (including 0 and k−1) integers. The string Str1 is called the prefix of the string Str2, when and only if: 1≤t≤m exists, making str1=str2[1..t]. where m is the length of the string Str2, str2[1..t] represents the string consisting of the first T characters of STR2.InputThe 1th line of the input file contains 2 positive integer n,k, separated by a single space, to represent a total of n words, which need to be replaced with a K-binary string. Next n lines, line i+1 contains 1 non-negative integer WI, indicating the number of occurrences of the first word.OutputThe output file consists of 2 rows.  Line 1th outputs 1 integers, the shortest length after the "Homer Epic" has been re-encoded. The 2nd line outputs 1 integers, the shortest length of the longest string si, in case the shortest total length is guaranteed.Sample Input4 2
1
1
2
2Sample Output12
2HINTX (k) indicates that x is a string in K-binary notation. An optimal scheme: 00 (2) replaces the 1th Word, 01 (2) replaces the 2nd Word, 10 (2) replaces the 3rd Word, and 11 (2) replaces the 4th Word.  In this scenario, the shortest length after encoding is: 1x2+1x2+2x2+2x2=12 the length of the longest string Si is 2. A non-optimal scenario: 000 (2) replaces the 1th Word, 001 (2) replaces the 2nd Word, 01 (2) replaces the 3rd Word, and 1 (2) replaces the 4th Word. In this scenario, the shortest length after encoding is: 1x3+1x3+2x2+2x1=12 the length of the longest string Si is 3.   The length of the article is the same as the optimal scheme, but the longest string length is longer.  For all data, guarantee 2≤n≤100000,2≤k≤9. Players note that 64-bit integers are used for input, output, storage, and calculation.SourceSolution

The topic background has already hinted that this problem needs to use Huffman code

A word summary (from Baidu Encyclopedia): in the variable word length encoding, if the code character lengths in strict accordance with the corresponding symbols appear in reverse order of probability size, the average code word length is the smallest

So we construct the Huffman tree from the bottom up, the greedy to take the minimum value of the $k$ node is reconstructed into a node, the point weights are the points of these points right and can be implemented with $priority$_$queue$

In order to do the second question, we can $pair$ in the priority queue, the first element is the number of occurrences, the second element is the longest length, so that the length of each selection can be as small as possible to get the best solution

When $k\neq 2$, the perfect merger (perceptual understanding of the word) needs to meet $n\equiv 1\ (mod\ k-1) $, which is equivalent to adding a few more occurrences of $0$ words when not satisfied with this condition

So $k\neq 2$ when we first complement the value of $0$ nodes so that can be perfectly merged, then greedy can

1#include <bits/stdc++.h>2 #defineFIR First3 #defineSEC Second4 using namespacestd;5typedefLong Longll;6typedef PAIR&LT;LL, Ll>PLL;7PRIORITY_QUEUE&LT;PLL, Vector<pll>, greater<pll> >PQ;8 intMain ()9 {Ten     intN, K; One PLL x, y, ans; ACIN >> N >>K; -      for(inti =1; I <= N; ++i) -     { theCIN >>X.first; - Pq.push (x); -     } -      while(k >2&& n% (K-1) !=1) +Pq.push (y), + +N; -      while(Pq.size ()! =1) +     { AY.fir = Y.sec =0; at          for(inti =1; I <= K; ++i) -         { -x =pq.top (), Pq.pop (); -Y.fir + =X.fir; -Y.sec = Max (y.sec, X.sec +1); -         } inAns.fir + =Y.fir; -Ans.sec =Max (ans.sec, y.sec); to Pq.push (y); +     } -cout << ans.fir << endl << ans.sec <<Endl; the     return 0; *}
View Code

[BZOJ4198] [Noi2015] Homer Epic (greedy)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.