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,'\n');
out(ans2,'\n');
return 0;
}
标签:P2168,荷马史诗,ans1,sum,ans2,NOI2015,read,lxl,mx 来源: https://www.cnblogs.com/QQ2519/p/15466610.html