[Swust OJ 352]--Merge fruit (greedy + queue simulation)

Source: Internet
Author: User

Title Link: http://acm.swust.edu.cn/problem/352/

Time limit (ms): $ Memory Limit (KB): 65535 Description

In an orchard, Toto has beaten down all the fruits and divided them into different heaps according to the different kinds of fruit. A lot decided to synthesize all the fruits. Each time a merger, a lot can combine the two fruits together, the energy consumed equals two of the weight of the sum of the fruit. It can be seen that all the fruit after the N-1 merger, there is only a pile. The total amount of energy consumed in the merging of fruits equals the physical strength of each merger. Because it's going to take a lot of effort to bring the fruit home, you need to save as much as you can when you combine the fruit. Assuming that each fruit has a weight of 1, and that the number of fruits and the number of each fruit is known, your task is to design a combination of sequential schemes that will consume the least amount of energy and output this minimum physical cost. For example, there are 3 kinds of fruit, the number is 1,2,9. The 1 and 2 stacks can be merged first, and the new heap number is 3, which consumes 3 of the energy. Next, the new heap is merged with the original third heap, and a new heap is obtained, with a number of 12, which consumes 12 of the energy. So the total cost of energy =3+12=15. It can be shown that 15 is the minimum physical cost.

Input

The input consists of two lines, the first line being an integer n (1<=n<=10000), which represents the number of species of fruit. The second line contains n integers, separated by spaces, and the first integer AI (1<=ai<=20000) is the number of fruit of the first I.

Output

The output includes one row, which contains only an integer, which is the minimum physical cost. The input data guarantees that this value is less than 2^31.

Sample Input
31 2 9
Sample Output
15

For 30% of data, guaranteed to have n<=1000: for 50% of data, guaranteed to have n<=5000; for all data, guaranteed to have n<=10000

Problem-solving ideas: Obviously a greedy problem, the first to sort the data in ascending order, and then press test instructions greedy good, but pay attention to merge the value of also to be included in the greedy range, every time the sort words certainly unrealistic,

Here the use of analog queue, clever data added to the queue at the end, two sets of scalar, save time and space, the specific can be referred to the code under the understanding ~ ~ ~

The code is as follows:

1#include <iostream>2#include <algorithm>3 using namespacestd;4 intMain ()5 {6     intstack[10001], n, I;7      while(Cin >>N) {8          for(i =0; I < n; i++) Cin >>Stack[i];9Sort (stack, stack +n);Ten         intFntA =0, Enda = n, fntb =0, endb =0, sum =0, J; One          while(1){ A             if(FntA = = Enda&&fntb +1==endb) -                  Break; -             intCur =0; the              for(j =0; J <2; J + +){ -                 if(FntA! =Enda) { -                     if(Fntb! =endb) -Cur + = Stack[fnta] < STACK[FNTB]? stack[fnta++]: stack[fntb++]; +                     ElseCur + = stack[fnta++]; -                 } +                 ElseCur + = stack[fntb++]; A             } atSum + =cur; -stack[endb++] =cur; -         } -cout << sum <<Endl; -     } -     return 0; in}
View Code

[Swust OJ 352]--Merge fruit (greedy + queue simulation)

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.