1. 程式人生 > >UOJ#130 【NOI2015】荷馬史詩 K叉哈夫曼樹

UOJ#130 【NOI2015】荷馬史詩 K叉哈夫曼樹

i++ getchar ext getch pre user sum spec getc

【NOI2015】荷馬史詩

鏈接:http://uoj.ac/problem/130

因為不能有前綴關系,所以單詞均為葉子節點,就是K叉哈夫曼樹。第一問直接求解,第二問即第二關鍵字為樹的高度。

#include< cstdio >
#include< algorithm >

typedef unsigned long long ull;
template
inline void read(T&x)
{
	x=0;bool f=0;char c=getchar();
	while((c<‘0‘||c>‘9‘)&&c!=‘-‘)c=getchar(); if(c==‘-‘)f=1,c=getchar();
	while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
	x=f?-x:x;
}
const int MAXN(100010);
struct Data
{
	ull val;int dep;
inline bool operator <(const Data &A)const {return (val==A.val)?dep>A.dep:val>A.val;}
}h[MAXN],New;int top;
int n,k,First;ull Ans,sum,w[MAXN];
void umax(int &a,int b){if(a<b)a=b;}
int main()
{
//	freopen("C.in","r",stdin);
//	freopen("C.out","w",stdout);
	read(n);read(k);
	for(int i=1;i<=n;i++)read(w[i]),sum+=w[i];
	for(int i=1;i<=n;i++)h[++top]=(Data){w[i],1};
	std::make_heap(h+1,h+1+top);
	First=(n-1)%(k-1) +1;
//	printf("%d\n",First);
//	printf("%llu\n",sum);
	if(First>1||n==1)
	{
	for(int i=1;i<=First;i++)
	{
		New.val+=h[1].val;
		Ans+=h[1].val;
		umax(New.dep,h[1].dep);
		std::pop_heap(h+1,h+1+top);top--;		
	}
	New.dep++;h[++top]=New;
	std::push_heap(h+1,h+1+top);
	}
	while(top>1)
	{
		New.val=New.dep=0;
		for(int i=1;top&&i<=k;i++)
		{
			New.val+=h[1].val;
			Ans+=h[1].val;
			umax(New.dep,h[1].dep);
			std::pop_heap(h+1,h+1+top);top--;
		}
		New.dep++;
//		printf("%llu %d\n",New.val,New.dep);
		h[++top]=New;
		std::push_heap(h+1,h+1+top);
	}
	printf("%llu\n",Ans);
	printf("%d\n",h[1].dep-1);
	return 0;
}

UOJ#130 【NOI2015】荷馬史詩 K叉哈夫曼樹