【洛谷P2168】【NOI2015】—荷马史诗(哈夫曼树)

传送门


水题
做哈夫曼树的时候维护一下深度最小即可

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
const int mod=998244353,g=3;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=100005;
priority_queue<pii,vector<pii>,greater<pii> > q;
int ans,n,k;
signed main(){
	n=read(),k=read();
	for(int i=1;i<=n;i++)q.push(pii(read(),1));
	int res=(n-1)%(k-1);
	if(res!=0)for(int i=1;i<=k-1-res;i++)q.push(pii(0,1));
	while(q.size()>1){
		int val=0,dep=0;pii now;
		for(int i=1;i<=k;i++){
			now=q.top();q.pop();
			val+=now.fi,chemx(dep,now.se);
		}
		ans+=val,q.push(pii(val,dep+1));
	}
	cout<<ans<<"\n"<<q.top().se-1<<'\n';
}
posted @ 2019-08-21 18:29  Stargazer_cykoi  阅读(91)  评论(0编辑  收藏  举报