"NOI2015" Homer

Portal
Luogu

Problem-solving ideas

\ (K \) fork \ (\ text {Huffman} \ ) tree board title, as the shortest longest string as long as the same weight of the small depth priorities like.

Details Notes

  • Gugu Gu

Reference Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= c == '-', c = getchar();
    while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
    s = f ? -s : s;
}

typedef long long LL;
const int _ = 100010;

int n, k; struct node{ LL w; int h; };
inline bool operator < (const node& a, const node& b)
{ return a.w == b.w ? a.h > b.h : a.w > b.w; }
priority_queue < node > Q;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
#endif
    read(n), read(k);
    for (rg int i = 1; i <= n; ++i) {
        LL w; read(w), Q.push((node) { w, 1 });
    }
    while ((n - 1) % (k - 1) != 0) Q.push((node) { 0, 1 }), ++n;
    LL ans = 0;
    while (Q.size() != 1) {
        int mx = 0; LL tmp = 0;
        for (rg int i = 1; i <= k; ++i)
            tmp += Q.top().w, mx = max(mx, Q.top().h), Q.pop();
        ans += tmp, Q.push((node) { tmp, mx + 1 });
    }
    printf("%lld\n%d\n", ans, Q.top().h - 1);
    return 0;
}

End Sahua \ (qwq \)

Guess you like

Origin www.cnblogs.com/zsbzsb/p/11751348.html