「NOI2015」荷马史诗

传送门
Luogu

解题思路

(k)( ext{Huffman}) 树板子题,至于最长串最短,只要同样权值的优先考虑深度小的就好了。

细节注意事项

  • 咕咕咕

参考代码

#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
%d
", ans, Q.top().h - 1);
	return 0;
}

完结撒花 (qwq)

原文地址:https://www.cnblogs.com/zsbzsb/p/11751348.html