HYSBZ 4198 Homer Epic

Source: Internet
Author: User
Tags time limit
4198 Homer Epic

Time Limit:10 Sec Memory limit:512 MB
submit:1574 solved:828
[Submit] [Status] [Discuss] 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-binary string si, which satisfies 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 case 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 Input

4 2
1
1
2
2 Sample Output

12
2 HINT

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. Test Instructions

Replace the original word with the K-binary string, and the shortest length of the longest string, with the minimum string length and the minimum total length guaranteed. Thinking of solving problems

K-Fork Huffman Tree.

Do not need to build, with priority queue processing can.

The code is as follows

#include <stdio.h> #include <iostream> using namespace std;
    #include <queue> #define MAXN 100005 struct Node {long long x, y;
        Node () {} node (long long A,long long b) {x=a;
    Y=b;
        } friend bool operator< (node A,node b) {if (a.x==b.x) return a.y>b.y;
    Return a.x>b.x;
}
};

Priority_queue<node> Q;
    int main () {//Freopen ("In.txt", "R", stdin);
    int n,k;
    scanf ("%d%d", &n,&k);
        for (int i=0; i<n; i++) {Long long tmp;
        scanf ("%lld", &tmp);
    Q.push (Node (tmp,0));
            } if (k!=2) while (n% (k-1)!=1) {Q.push (node (0,0));
        n++;
    } Long long ans=0;
        while (Q.size () >1) {Long long sum,deep;
        Sum=deep=0;
            for (int i=0; i<k; i++) {sum+=q.top (). x;
            Deep=max (Deep,q.top (). y);
        Q.pop ();
        } ans+=sum; Q.pusH (node (sum,deep+1));
    } printf ("%lld\n%lld\n", Ans,q.top (). y);
return 0;
 }

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.