P2168 [NOI2015] 荷马史诗

用哈夫曼树的思想,每次取出k小,合成。注意要加些0使得最后成为满k叉树

const int N=1e5+79;
lxl n,k;

struct node{
	lxl w,d;
	bool operator <(const node &x)const {
	if(w!=x.w) return w>x.w;else return d>x.d;}
}; 
std::priority_queue<node> q;

int main() {
	read(n);read(k);
	lxl x;
	rep(i,1,n){
		read(x);
		q.push({x,1});
	}
	while((n-1)%(k-1)){
		q.push({0,1});++n;
	}
	
	lxl sum(0),mx(0),ans1(0),ans2(0);
	while(q.size()>1){
		
		sum=0;mx=-1;
		rep(i,1,k){
			sum+=q.top().w;
			mx=max(mx,q.top().d);
			q.pop();
		}
		q.push({sum,mx+1});
		ans1+=sum;
		ans2=max(ans2,mx);
	}
	out(ans1,'
');
	out(ans2,'
');
	
	
	return 0;
}

本文来自博客园,作者:{2519},转载请注明原文链接:https://www.cnblogs.com/QQ2519/p/15466610.html

原文地址:https://www.cnblogs.com/QQ2519/p/15466610.html