# 多元Huffman编码问题

``````7 3
45 13 12 16 9 5 22``````

``593 199``

``````#include<iostream>
#include<cstdio>
#include<list>
#include<queue>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int n, k, total, maxVal, minVal, temp, num, p[100005];
multiset<int> mySet;

void solve()
{
while (mySet.size() != 1)
{
temp = 0;
for (int i=0; i<k; i++)
{
temp += *mySet.begin();
mySet.erase(mySet.begin());
}
mySet.insert(temp);
minVal += temp;
}
}

int main()
{
//while (scanf("%d %d", &n, &k) != EOF)
scanf("%d %d", &n, &k);
{
total = temp = minVal = maxVal = 0;
mySet.clear();
for (int i=0; i<n; i++)
{
scanf("%d", &num);
p[i] = num;
mySet.insert(p[i]);
total += num;
}
if (k > n) k = n;
temp = 0;
sort(p, p+n);
for (int i=n-1; i>=0; i--)
{
temp += p[i];
if (i != n-1) maxVal += temp;
}
while (mySet.size() % (k - 1) != 1 && k != 2) mySet.insert(0);
solve();
printf("%d %d\n", maxVal, minVal);
}
return 0;
}
``````

``````#include<iostream>
#include<cstdio>
#include<list>
#include<queue>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int n, k, total, maxVal, minVal, temp, num, p[100005];
multiset<int> mySet;

void solve()
{
while (mySet.size() != 1)
{
temp = 0;
for (int i=0; i<k; i++)
{
temp += *mySet.begin();
mySet.erase(mySet.begin());
}
mySet.insert(temp);
minVal += temp;
}
}

int main()
{
//while (scanf("%d %d", &n, &k) != EOF)
scanf("%d %d", &n, &k);
{
total = temp = minVal = maxVal = 0;
mySet.clear();
for (int i=0; i<n; i++)
{
scanf("%d", &num);
p[i] = num;
mySet.insert(p[i]);
total += num;
}
if (k > n) k = n;
temp = 0;
sort(p, p+n);
for (int i=n-1; i>=0; i--)
{
temp += p[i];
if (i != n-1) maxVal += temp;
}
while (mySet.size() % (k - 1) != 1 && k != 2) mySet.insert(0);
solve();
printf("%d %d\n", maxVal, minVal);
}
return 0;
}
``````

1. 看看说道：