Sdut Heap Structure exercises -- combine the fruit of the heap tree)

Source: Internet
Author: User
Tree-Heap Structure exercise-combine the fruit of the Harman tree Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ In an orchard, many fruits have been laid down and divided into different heaps based on different types of fruits. I decided to combine all the fruits into a pile. For each merge, you can combine two fruit sets. The physical strength consumed is equal to the sum of the weights of the two fruit sets. It can be seen that after all the fruits have been merged for n-1 times, there is only one pile left. The total amount of physical strength consumed when Merging Results is equal to the sum of physical strength consumed by each merge. Because we need to make great effort to move these fruits home, we need to save as much effort as possible when merging them. Assuming that each fruit has a weight of 1 and the number of known fruit types and the number of each fruit, your task is to design a combined sequence scheme to minimize the amount of effort required, and output the minimum physical labor consumption value. For example, there are three fruit types in sequence: 1, 2, and 9. You can merge 1 and 2 heaps first. The number of new heaps is 3, and the physical strength is 3. Next, merge the new heap with the original third heap and obtain the new heap. The number is 12, which consumes 12 resources. Therefore, the total amount of physical strength is 3 + 12 = 15. It can prove that 15 is the minimum physical labor cost. The first line of the input is an integer N (1 <=n <= 10000), indicating the number of fruit types. The second line contains N integers separated by spaces. the I-th AI (1 <= AI <= 20000) is the number of I-th fruits. The output includes a row. This row contains only one integer, that is, the minimum physical consumption value. The input data must be less than 2 ^ 31. Sample Input
31 2 9
Sample output
15
STL is more and more lost .. Heap (HEAP) has a total of four operations, which are generally implemented with a vector base. Assume that vector <int>;
Operation ①: make_heap (A. Begin (), A. End () Think twice to create a heap. The default value is the maximum heap. You can add the third parameter to modify the heap.
Operation ②: pop_heap (. begin (),. end () deletes the header node. In fact, It swaps the position of the header and the end to (. begin (),. end ()-1) to re-build the heap for the interval, You need to manually delete the last element (. pop_back ());
Operation ③: push_heap (. begin (),. end () adds an element to the heap. Before this operation, you must first press the element into the container (. push_back ())
Operation ④: sort_heap (A. Begin (), A. End () Heap sorting. (Never used)
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # include <vector> # include <queue> using namespace STD; const int INF = 0x3f3f3f3f; const int maxn = 1e6; vector <int> A; int main () {int n, x; scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% d", & X);. push_back (x);} make_heap (. begin (),. end (), greater <int> (); // minimum heap. The default value is the maximum heap int ans = 0. While (. size ()! = 0) {int x =. front (), Y = 0; pop_heap (. begin (),. end (), greater <int> ();. pop_back (); if (. size ()! = 0) {Y =. front (); pop_heap (. begin (),. end (), greater <int> ();. pop_back ();} ans + = (x + y); if (. size ()! = 0) {. push_back (x + y); push_heap (. begin (),. end (), greater <int> () ;}} printf ("% d \ n", ANS); Return 0 ;}

This question can also be implemented using the priority queue.
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#include <queue>using namespace std;const int INF=0x3f3f3f3f;const int maxn=1e4;struct cmp{bool operator ()(int a,int b){return a>b;}};priority_queue <int,vector<int>,cmp> Q;int main(){    int n,x;    scanf("%d",&n);    while(n--){scanf("%d",&x);Q.push(x);}int ans=0;while(!Q.empty()){int a=0,b=0;a=Q.top();Q.pop();if(!Q.empty()){b=Q.top();Q.pop();}if(!Q.empty())Q.push(a+b);ans+=(a+b);}printf("%d\n",ans);return 0;}


Sdut Heap Structure exercises -- combine the fruit of the heap tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.