Merge fruit, merge fruit C Language
§ Combine fruit (fruit) [Problem description] in an orchard, a lot of fruit has been put down and divided into different heaps based on different types of fruit. 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 data structure is clear. The operation to be performed in the problem is: (1) Getting the smallest number from a table (2) Inserting a number into the table. Supports the dynamic query of the minimum number and the data structure of the dynamic insert operation. We can choose to use the heap. Because the smallest element is used, we need to implement it with a small root heap. The key part of heap is two operations: put operation, that is, adding an element to the heap; get operation, that is, removing an element from the heap. § 3. Create a small root heap through n put operations at the beginning of the entire program, and then repeat the following operations: the two get operations get the two minimum numbers and accumulate them, create a new node and insert it into the heap. For example, 1 + 1 = 2, insert 2 to the back of the heap, and then adjust it from the bottom up so that the array including 2 satisfies the nature of the heap.
1 # include & lt; iostream & gt; 2 # include & lt; algorithm & gt; 3 using namespace std; 4 int a [100001]; 5 int tot = 0; 6 int main () 7 {8 int n; 9 cin> n; 10 for (int I = 1; I <= n; I ++) 11 {12 cin> a [I]; 13} 14 make_heap (a + 1, a + n + 1, greater <int> (); // create a small root heap 15 int m = n; // retrieve the n size for convenient maintenance of 16 for (int I = 1; I <n; I ++) 17 {18 int a1 = a [1]; // obtain the minimum element 19 pop_heap (a + 1, a + m + 1, greater <int> ()); // Delete the smallest and maintain 20 int a2 = a [1]; // The second smallest element 21 pop_heap (a + 1, a + m, greater <int> ()); // fetch and maintain the second smallest sum, 22 int sum = a1 + a2; // calculate the required physical strength 23 tot = tot + sum; 24 a [M-1] = sum; // put the generated element back to 25 push_heap (a + 1, a + m, greater <int> (); // maintain 26 m again --; 27} 28 29 cout <tot; 30 return 0; 31}