题意:传送门
题解:构造方式就是构造huffman编码,把单词的出现次数作为叶子节点的权值,求出k叉huffman树,构造方式可以添加0点,使得
(
n
−
1
)
%
(
k
−
1
)
=
=
0
(n-1)\%(k-1)==0
(n−1)%(k−1)==0,最后还有个条件就是使得最长的
S
i
Si
Si尽量短,那么就是提前让深度低的构树。
c
o
d
e
:
code:
code:
#include<bits/stdc++.h>
#define ll long long
#define pli pair<long long,int>
using namespace std;
const int N=100000+10;
int n,k;
ll res=0;
int main()
{
scanf("%d%d",&n,&k);
priority_queue<pli,vector<pli>,greater<pli> >heap;
for(int i=0;i<n;i++){
ll x;
scanf("%lld",&x);
heap.push({x,0});
}
while((n-1)%(k-1)!=0){heap.push({0,0});n++;}
while(heap.size()>1){
ll s=0;
int depth=0;
for(int i=0;i<k;i++){
s+=heap.top().first;
depth=max(depth,heap.top().second);
heap.pop();
}
res+=s;
heap.push({s,depth+1});
}
printf("%lld\n%d\n",res,heap.top().second);
return 0;
}