Lazy Xiao Ming time limit:MS | Memory limit:65535 KB Difficulty:3
-
Describe
-
Xiao Ming wants to eat fruit, just as the orchard fruit is ripe. In the orchard, Xiaoming has beaten down all the fruits and divided them into different piles according to the different kinds of fruit. Xiao Ming decided to synthesize all the fruits into a pile. Because Xiao Ming is lazy, in order to save energy, Xiao Ming began to think of ideas:
Each time, Xiao Ming can combine the two piles of fruit and consume the same amount of energy as the sum of the weight of two of the fruit. It can be seen that all the fruit after the N-1 merger, there is only a pile. The total energy consumed by xiaoming during the merging of fruits equals the physical strength of each merger.
Because it will take a lot of effort to carry these fruits home, so Xiao Ming in the combination of fruit as much as possible to save energy. 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 the sequence, so that xiaoming consumes the least amount of energy, and output this minimum energy consumption value.
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 Xiaoming spent a total of energy =3+12=15. It can be shown that 15 is the minimum physical cost.
-
-
Input
-
The
-
first line of input integer N (0<n<=10) indicates the number of test data groups. Next, each set of test data inputs consists of two lines, the first of which is an integer n (1<=n<=12000), which indicates the number of species of the 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
-
-
each set of test data output includes one row, which contains only an integer, which is the minimum physical cost.
-
-
Sample input
-
-
13 1 2 9
-
-
Sample output
-
-
15
-
-
Source
-
-
[HZYQAZASDF] Original
-
-
Uploaded by
-
-
HZYQAZASDF
-
-
//Array implementation priority queue;
-
1#include <stdio.h>2#include <algorithm>3 using namespacestd;4 Long Long intnum[12020];5 intMain ()6 {7 intT;8scanf"%d",&t);9 while(t--)Ten { One intI, J, M; Ascanf"%d", &m); - for(i=0; i<m; i++) -scanf"%lld", &num[i]); theSort (num, num+m); - Long Long intTotal =0; - for(i=0; i<m; i++) - { + if(i = = m1) - Break; + Long Long inttemp = num[i]+num[i+1]; Anum[i+1] =temp; atTotal + =temp; - for(j=i+1; J < M; J + +) - { - if(Num[j] < num[j-1]) - { - Long Long ints; ins = num[j]; NUM[J] = num[j-1]; num[j-1]=s; - } to } + } -printf"%lld\n", total); the } * return 0; $}
Priority queue;
1#include <queue>2#include <cstdio>3#include <iostream>4 using namespacestd;5 intMain ()6 {7 intT;8scanf"%d", &t);9 while(t--)Ten { Onepriority_queue<int, vector<int, greater<int> >Q; A intI, M; -scanf"%d", &m); - for(i=0; i<m; i++) the { - intnum; -scanf"%d", &num); - Q.push (num); + } - Long Long intTotal =0; + while(Q.size ()! =1) A { at Long Long inttemp =q.top (); - Q.pop (); -Temp + =q.top (); - Q.pop (); - Q.push (temp); -Total + =temp; in } -printf"%lld\n", total); to } + return 0; -}
Nanyang 55--Province Lazy Matter of xiaoming