Merge fruit (Huffman

# The meaning of problems
n heap fruit, fruit weight per bunch has combined small piles of fruit and strength equal to the weight of all the fruit through the combined n-1 times after the remaining pile, overall power consumption is equal to all final and the output of the minimum consumption of physical

# Explanations
i.e. huffman the tree, to achieve the heap, each taking two values of the minimum is stored again into the reactor together, the process of cumulative sum

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4    ios::sync_with_stdio(0);
 5    cin.tie(0);
 6    cout.tie(0);
 7    int n;
 8    cin>>n;
 9    priority_queue<int,vector<int>,greater<int>>a;
10    for(int i=0;i<n;i++){
11       int x;
12       cin>>x;
13       a.push(x);
14    }
15    int ans=0;
16    for(int i=1;i<n;i++){
17       int x=a.top();
18       a.pop();
19       int y=a.top();
20       a.pop();
21       a.push(x+y);
22       ans+=x+y;
23    }
24    cout<<ans;
25 }

 

 

Guess you like

Origin www.cnblogs.com/hhyx/p/12536324.html