-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1387.cpp
100 lines (81 loc) · 1.59 KB
/
1387.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// the construction of huffman
#include <iostream>
#include <limits.h>
using namespace std;
struct HuffmanNode
{
int height = 0;
long long weight = 0;;
int parent = 0;;
int left = 0, right = 0;
};
int minIndex(HuffmanNode Bt[], int k, int m)
{
int i, min = 0;
long long minWeight = LLONG_MAX;
for (i = m - 1; i > k; i--)
{
if (Bt[i].parent == 0 && Bt[i].weight < minWeight)
{
min = i;
minWeight = Bt[min].weight;
}
}
return min;
}
HuffmanNode* BestBinaryTree(int* w, int n)
{
HuffmanNode* BBTree;
int first_min, second_min;
int m = n * 2;
int i, j;
BBTree = new HuffmanNode[m];
for (j = 0; j < n; j++)
{
i = m - 1 - j;
BBTree[i].weight = w[j];
BBTree[i].parent = 0;
BBTree[i].left = 0;
BBTree[i].right = 0;
}
i = n - 1;
while (i != 0)
{
first_min = minIndex(BBTree, i, m);
BBTree[first_min].parent = i;
second_min = minIndex(BBTree, i, m);
BBTree[second_min].parent = i;
BBTree[i].weight = BBTree[first_min].weight + BBTree[second_min].weight;
BBTree[i].parent = 0;
BBTree[i].left = first_min;
BBTree[i].right = second_min;
i--;
}
for (int i = 1; i < n; i++)
{
int left = BBTree[i].left;
int right = BBTree[i].right;
BBTree[left].height = BBTree[i].height + 1;
BBTree[right].height = BBTree[i].height + 1;
}
return BBTree;
}
int main()
{
int N;
cin >> N;
int* list = new int[N];
for (int i = 0; i < N; i++)
{
cin >> list[i];
}
HuffmanNode* BBtree = BestBinaryTree(list, N);
long long sum = 0;
for (int i = N; i < 2 * N; i++)
{
sum += BBtree[i].height * BBtree[i].weight;
}
cout << sum;
delete[] list;
return 0;
}