Fruit migration (9du oj)

Source: Internet
Author: User

Preface this question took nearly half a month. During this period, I reviewed some basic knowledge, greedy algorithms, heap sorting, and I finally referred to my blog, finally, the problem of building the smallest priority queue ac through the smallest heap! I recommend my classmate's blog, the content is very good and people are very sharp: http://blog.csdn.net/cscmaker/article/details/8138870 questions [html] view plaincopy Title Description: in an orchard, James has all the fruit beat down, according to the different types of fruits, James decided to combine all the fruits into a pile. For each merge, James can combine two piles of fruits, and the physical strength consumed is equal to the sum of the weights of the two piles of fruits. Of course, after n-1 merge, it will become a pile. The total physical strength consumed by James in merging fruits is the sum of the physical strength consumed by each merge. 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 Xiao Ming's physical strength, and output the minimum physical labor consumption value. For example, there are three types of fruits, with numbers 1, 2, and 9 in sequence. You can merge 1 or 2 heaps. The number of new heaps is 3, and the physical strength is 3. Then combine the new heap with the original third heap to get the new heap, which consumes 12 resources. Therefore, James spent a total of 3 + 12 = 15, which can prove that 15 is the minimum physical labor cost. Input: each data input includes two rows. The first row is an integer n (1 <= n <= 10000), indicating the number of fruit types, if n is equal to 0, the input ends without processing. The second line contains n integers separated by spaces. the I-th INTEGER (1 <= ai <= 1000) is the number of I-th fruits. Output: for each group of input, an integer is output and a line break is generated. This value is the minimum physical consumption value. The input data must be less than 2 ^ 31. Sample input: 3 9 1 2 0 sample output: 15 ac code [cpp] # include <stdio. h> # include <stdlib. h> # define NUM 10001 void minHeapIfy (int * A, int I, int n); void buildMinHeap (int * A, int n); int heapExtractMin (int *, int n); void heapIncreaseKey (int * A, int I, int key); int moveFruit (int * A, int n); int main () {int I, n, power, weight [NUM]; while (scanf ("% d", & n )! = EOF & n! = 0) {// receives the client parameter for (I = 1; I <= n; I ++) {scanf ("% d", & weight [I]);} // greedy power = moveFruit (weight, n); printf ("% d \ n", power);} return 0;}/*** Description: maintain the minimum heap */void minHeapIfy (int * A, int I, int n) with I as the root node {int min, loc, r, l, change; for (min = I; min <= n;) {l = min * 2; r = min * 2 + 1; loc = min; if (l <= n & A [l] <A [min]) min = l; if (r <= n & A [r] <A [min]) min = r; if (loc! = Min) {change = A [min]; A [min] = A [loc]; A [loc] = change ;}else {break ;}}} /*** Description: Remove and return the smallest element in the heap */int heapExtractMin (int * A, int n) {int max = A [1]; A [1] = A [n]; minHeapIfy (A, 1, n-1); return max;}/*** Description: create the minimum heap */void buildMinHeap (int * A, int n) {int I; for (I = n/2; I> = 1; I --) {minHeapIfy (, i, n) ;}}/*** Description: insert the element to the least priority queue */void heapIncreaseKey (int * A, int I, int key) {int parent, change; for (A [I] = key, parent = I/2; I >=1 & A [parent]> A [I] & parent> = 1 ;) {change = A [parent]; A [parent] = A [I]; A [I] = change; I = parent; parent = I/2 ;}} int moveFruit (int * A, int n) {int power, I, lchild, rchild, parent, j; buildMinHeap (A, n); for (I = 1, j = n, power = 0; I <n; I ++) {lchild = heapExtractMin (A, j); j-= 1; rchild = heapExtractMin (A, j ); parent = lchild + rchild; power + = lchild + rchild; heapIncreaseKey (A, j, parent);} return power ;}

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.