-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0148.h
More file actions
51 lines (45 loc) · 1.03 KB
/
Problem0148.h
File metadata and controls
51 lines (45 loc) · 1.03 KB
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
48
49
50
51
//
// Created by Fengwei Zhang on 2021/6/25.
//
#ifndef ACWINGSOLUTION_PROBLEM0148_H
#define ACWINGSOLUTION_PROBLEM0148_H
#include <iostream>
#include <queue>
using namespace std;
class Problem0148
{
// 霍夫曼树,不要误认为是"石子合并问题"(AcWing0282)
private:
int huffman_tree(const int *items, const int &n)
{
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < n; ++i)
{
pq.emplace(items[i]);
}
int result = 0;
while (pq.size() > 1)
{
auto a = pq.top();
pq.pop();
auto b = pq.top();
pq.pop();
pq.emplace(a + b);
result += a + b;
}
return result;
}
int main()
{
int n;
scanf("%d", &n);
int items[n];
for (int i = 0; i < n; ++i)
{
scanf("%d", &items[i]);
}
printf("%d\n", huffman_tree(items, n));
return 0;
}
};
#endif // ACWINGSOLUTION_PROBLEM0148_H