bzoj4198 [Noi2015] [homer Epic] Huffman code

Source: Internet
Author: User

1. Maintain K-fork tree, pay attention to 0
2. Use the heap to maintain the minimum frequency and length, and each time you merge up, statistical answer
3 when merging. if N (k-1) =1 then do not need to fill 0 (because each merge is equivalent to adding K-1 nodes, all if you can merge into one node, Then must meet the above conditions)

# include <queue> # include <cstdio> # include <cstring> # include <iostream> # include <algori

Thm> # define LL Long long using namespace std;
    Inline LL Read () {LL x = 0, f = 1;
    char ch = getchar ();
        while (! isdigit (CH)) {if (ch = = '-') f =-1LL;
    ch = getchar ();
        } while (IsDigit (CH)) {x = 1LL * x * + ch-' 0 ';
    ch = getchar ();
} return x * f;
    } struct p{LL len, P; P (ll x = 0, ll y = 0): Len (x), p (y) {} bool operator< (const P & RHS) const {if (p = = RH
        S.P) return len > Rhs.len;
    return p > rhs.p;

}} an;

Priority_queue <P> Q;
    int main () {//Freopen ("Debug.in", "R", stdin);
    LL N, K;
    n = Read (), k = Read ();
        for (int i = 1; I <= n; + + i) {LL x = Read ();
    Q.push (P (0, X));
       } while (K > 2 && n% (k-1)! = 1) { n + +;
    Q.push (P (0, 0));
        } while (Q.size () > 1) {int p = k;
        ll ll = 0, pp = 0; while (P &&!)
            Q.empty ()) {P E = Q.top ();
            Q.pop ();
            LL = max (ll, E.len + 1), pp + = E.P;
        P--;
        } an.p + = pp, An.len = max (An.len, LL);
    Q.push (P (ll, pp));
    } P E = Q.top ();
    printf ("%lld\n%lld", AN.P, An.len);
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.