AcWing 148. Consolidated fruit involves Huffman number for the first time

AcWing 148. Merged Fruit

In an orchard, Dada had already knocked down all the fruits, and divided them into different piles according to different types of fruits.

Dada decided to combine all the fruits into one pile.

Each time it is merged, Dada can merge two piles of fruits together, and the energy consumed is equal to the sum of the weight of the two piles of fruits.

It can be seen that after all the fruits are merged n-1 times, there is only one pile left.

The total stamina consumed by Dada when merging fruits is equal to the sum of the stamina consumed for each merge.

Because it takes great effort to move these fruits home, Dada should save as much energy as possible when merging the fruits.

Assuming that the weight of each fruit is 1, and the number of types of fruit and the number of each fruit are known, your task is to design a combined sequence plan to minimize the amount of physical effort required to reach the goal, and output the minimum value of physical effort .

For example, there are 3 kinds of fruits, the numbers are 1, 2, and 9.

You can merge piles 1 and 2 first, the number of new piles is 3, and the physical effort is 3.

Then, merge the new pile with the original third pile, and get a new pile with a number of 12 and a physical effort of 12.

So Dada expends a total of energy = 3 + 12 = 15.

It can be proved that 15 is the minimum physical cost.

Input format The
input consists of two lines. The first line is an integer n, indicating the number of fruit types.

The second line contains n integers, separated by spaces. The i-th integer ai is the number of the i-th fruit.

Output format The
output consists of one line, this line contains only an integer, which is the minimum physical exertion value.

Input data to ensure that this value is less than 231.

Data range
1≤n≤10000,
1≤ai≤20000
Input example:

3 
1 2 9

Sample output:

15

Ideas
This regards it as a binary tree. The number farthest from the root node is merged (the number of times of addition) the most, so the two smallest points are selected first, and then they become the root nodes of these two points. These points are selected from the smallest two points and are continuously updated, and finally merged into a pile.
Main idea
Huffman tree

We need to use the small root heap to do this problem. The small root heap finds the two smallest trees, and then updates the heap. When the heap size is finally updated to 1, the merge is over and we need to calculate the consumption of the merge.

This question is very similar to merging stones, but merging stones are adjacent stones. This merger is not necessarily adjacent, it is random.

#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

int main(void)
{
    
    
    int n;
    cin>>n;
    priority_queue<int,vector<int>,greater<int>> heap;
    while(n--)
    {
    
    
        int w;
        cin>>w;
        heap.push(w);
    }
    int res=0;
    while(heap.size()>1)
    {
    
    
        int a=heap.top();
        heap.pop();
        int b=heap.top();
        heap.pop();
        res+=a+b;
        heap.push(a+b);
    }
    cout<<res;
}

Guess you like

Origin blog.csdn.net/qq_52358098/article/details/113994372