Multivariate Huffman coding problem - priority queue

You see me you Da Buda
This link: https://blog.csdn.net/KO812605128/article/details/102604583

Multivariate Huffman coding problem
Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description
around a playground are placed n heap of stones. Stone is to have the order to merge into a pile. Provisions of each election at least 2 stacks up to the election k heap of stones into a new pile, the combined cost of the number of new stones pile. Try to design an algorithm to calculate the n combined into a pile of stones piled maximum and minimum total cost of the total cost.
For a given n gravel pile, to a maximum combined total cost is calculated and a minimum total cost of the pile.

Input
line of the first input data are two positive integers n and k (n≤100000, k≤10000), n represents stones piled there, each stack 2 is selected from selected from at least k stones piled up combined. The second row has a number n (the number of each not more than 100), respectively, represent the number of each pile of stones.

Output
The calculated maximum and minimum total cost of the total cost of the output, with a space between two integers.

Sample Input

7 3
45 13 12 16 9 5 22

Sample Output

593 199

Priority queue used here, learn on their own
priority queue priority_queue Comments

求最大费用时只需将石堆排列后从大到小,两两进行合并即可
求最小费用时,将石堆排列后,要尽可能的合并最少的次数且每次合并的石堆数为K堆
在求最小费用时,有时会出现特例,即每次合并K堆,最后一次合并时无法以K堆进行合并,
这样的话合并的结果就不是最小费用了,我们要将最小的堆合并最多次这样结果才会最小,
所以就要先判断原总堆数是否能使每次合并的堆数都为K堆,
如果不能的话就要在原堆数前面加上 X 个个数为0的堆来补齐缺少的堆数
例如共7堆最大合并5堆
石堆数 45 13 12 5 9 22 16
这时排序后为5 9 12 13 16 22 45
如果先合并前5堆 这样结果就为177
如果补零的话 0 0 5 9 12 13 16 22 45,每次合并K堆,结果为148

Example, sorting 5, 9, 12, 13, 16, 22, 45

最小:
判断是否需要添加0,不需要,进入加和,5 + 9 + 12 = 26;sum1 = 26,26进队;
13, 16, 22,26, 45;13 + 16 + 22 = 51;sum1 = 26 + 51 = 77,51进队;
26, 45, 51,26 + 45 + 51 = 122;sum1 = 77 + 122 = 199,结束。
最大:
45, 22, 16, 13, 12, 9, 5 ;
45 + 22 = 67;sum = 67,sum2 = 67,67进队;
67 + 16 = 83;sum = 67 + 16 = 83;sum2 = 83 + 67 = 150;150进队;
83 + 13 = 96;sum = 96 + 150 = 246......

Here Insert Picture Description

#include<bits/stdc++.h>

using namespace std;

int main()
{
  int n, k;
  cin>>n>>k;
  priority_queue<int, vector<int>, greater<int> > q1;///greater是从小到大
  priority_queue<int, vector<int>,less<int> >q2;///less是从大到小

  for(int i = 0; i < n; i++)
  {
      int x;
      cin>>x;
      q1.push(x);
      q2.push(x);
  }
  long long sum1 = 0, sum2  = 0;
  while(q1.size() % (k - 1) != 1)
      q1.push(0);///添加0
  while(q1.size() > 1)
  {
      long long sum = 0;
      for(int i = 0; i < k; i++)
      {
          sum += q1.top();
          q1.pop();
      }
      sum1 += sum;
      q1.push(sum);
  }
  while(q2.size() > 1)
  {
      long long sum = 0;
      int a = q2.top();
      q2.pop();
      int b = q2.top();
      q2.pop();
      sum += (a + b);
      //cout<<"sum = "<<sum<<endl;
      sum2 += sum;
      //cout<<"sum2 = "<<sum<<endl;
      q2.push(sum);
  }
  cout<<sum2<<" "<<sum1<<endl;
  return 0;
}

Guess you like

Origin blog.csdn.net/KO812605128/article/details/102604583