luogu2168 [NOI2015]荷马史诗

OI 字符串
文章目录

对于我这种没学过哈夫曼树的人极不友好……

ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long ll;
int n, k;
ll ans;
struct Node{
ll w;
int d;
}nd[100015];
struct cmp{
bool operator()(const Node &a, const Node &b)const{
if(a.w!=b.w) return a.w>b.w;
else return a.d>b.d;
}
};
priority_queue<Node, vector<Node>, cmp> q;
int main(){
cin>>n>>k;
for(int i=1; i<=n; i++){
scanf("%lld", &nd[i].w);
nd[i].d = 1;
}
if((n-1)%(k-1)){
int t=k-1-(n-1)%(k-1);
while(t--)
nd[++n] = (Node){0, 1};
}
for(int i=1; i<=n; i++)
q.push(nd[i]);
while(n!=1){
Node tmp=(Node){0, 0};
for(int i=1; i<=k; i++){
Node u=q.top();
q.pop();
tmp.w += u.w; tmp.d = max(u.d, tmp.d);
n--;
}
tmp.d++;
ans += tmp.w;
q.push(tmp);
n++;
}
cout<<ans<<endl<<q.top().d-1;
return 0;
}