Description
The 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. Input
The 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. Output
The 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
2HINT
X (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. The following: Because to ensure that SI is not the prefix of SJ. So you can think of Huffman coding. And then found that when K=2 is the merging of fruit. In fact, K-fork tree is the same practice. Every time you go to the smallest combination of K. It is found that only when the (n-1)% (k-1) =0 can be merged exactly, so add k-1-(n-1)% (k-1) a virtual node with a weight of 0 height of 1. Then use the heap to maintain the minimum value is good. To make sure the maximum height is minimal, we add the height to the second keyword of the comparison.
#include <iostream> #include <cstdio> #include <algorithm> #include <queue>using namespace std; struct Use{long long v,h;}; BOOL operator< (use A,use b) {if (A.V!=B.V) return a.v>b.v; else return a.h>b.h; }priority_queue<use>q;int n,k,top (0); Long long ans (0), Hh,x;int Main () {scanf ("%d%d", &n,&k); for (int. i=1;i<=n;i++) {Use temp; scanf ("%lld", &x); temp.v=x;temp.h=1; Q.push (temp); } if ((n-1)% (k-1)!=0) top=k-1-(n-1)% (k-1); for (int. i=1;i<=top;i++) {Use temp; temp.v=0;temp.h=1; Q.push (temp); } top+=n; while (top!=1) {use A; Long Long Temp (0), Maxx (0); for (int i=1;i<=k;i++) {a=q.top (); temp+=a.v; Maxx=max (A.h,maxx); Q.pop (); } ans+=temp; a.v=temp;a.h=maxx+1; Q.push (a); Top-=k-1; } hh=q.top (). h-1; Cout<<ans<<endl<
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"NOI2015" "bzoj4198" "Homer Epic" "K-Fork Huffman Tree" "Greedy"