luogu 1090 merger fruit

The combined fruit
Title
[luogu1090]

Title Description

In an orchard, a lot has shot down all the fruit, and different types of fruit into different piles. We decided to put a lot of fruit all synthetic pile.

Each combined, fruit lots can be combined together piles, consume physical strength equal to the weight of the fruit and piles. As can be seen, all the fruits were combined after n-1 times, on the left a pile. When a lot of fruit combined total consumption of physical strength is equal to the sum of each combined consumed.

But also because these fruits make great efforts to move back home, so a lot to save energy during the merge fruit as possible.

Number and the number of each type of fruit Fruit wt assumed that each are 1, and it is known fruit, your task is to design a combined program order, that minimizes cost a lot of physical strength, physical and outputting the minimum cost value.

For example, three kinds of fruit, followed by the number 1,2,9. 1,2 stack may be first combined, the number of the new stack 3, 3 taxing. Subsequently, the new stack and the third stack combined original, and to give the new stack number is 12, 12 is taxing. Therefore, a lot of total taxing = 3 + 12 = 15. 15 can be shown that the minimum value of the physical cost.

Input
Input consists of two lines, the first line is an integer n (1 <= n <= 10000), represents the number of types of fruit. The second line contains n integers, separated by spaces, the integer i ai (1 <= ai <= 20000) is the number of i-th fruit.

Outputting
an output including line, which contains a single integer, i.e. the value of the minimum physical cost. The input data to ensure that this value is less than 2 ^ 31.

Sample input and output

Copy Input # 1
. 3
1 2. 9
Output # 1 Copy
1
Description / prompt
for 30% of data, ensure n≤1000:

For 50% of the data, ensure n≤5000;

For all of the data, ensure n≤10000.

 

analysis

Decision monotonous.


This question and [luogu1880 stone combined] similarities

But the two are completely different questions, this question can merge any two piles and stones can only merge adjacent merger.

But is this difference, the two different questions

For the fruit of the merger, each greedy can choose the smallest two piles, anyway, the combined number are the same, after the first merger in the face of the answers will contribute a little more, it must first merge small.

But for stone consolidation, because only merge adjacent piles can not be guaranteed to be a minimum of two piles, can not be greedy, consolidation is adjacent interval, the interval will be more suitable for regulatory action

We return to the fruit of the merger, the decision of this question is monotonous, each selection:

1. The two numbers is the minimum number of columns of the original

2. The merger had the smallest number two

3. A merger before, not a merger

We use two queues, a deposit had not merged, the merger had a deposit

Now had not merge sort

Since they are small to large, so after adding a certain ratio of large to join

It guarantees the monotonicity two queues

Code

 1 /************************
 2 User:Mandy.H.Y
 3 Language:c++
 4 Problem:luogu1090
 5 Algorithm: 
 6 ************************/
 7 #include<bits/stdc++.h>
 8 
 9 using namespace std;
10 
11 const int maxn = 1e4 + 5;
12 
13 int n,l1,r1,l2,r2;
14 long long ans;
15 long long q[maxn],a[maxn];
16 
17 template<class T>inline void read(T &x){
18     x = 0;bool flag = 0;char ch = getchar();
19     while(!isdigit(ch)) flag |= ch == '-',ch =  getchar();
20     while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch =  getchar();
21     if(flag) x = -x;
22 }
23 
24 template<class T>void putch(const T x){
25     if(x > 9) putch(x / 10);
26     putchar(x % 10 | 48);
27 }
28 
29 template<class T>void put(const T x){
30     if(x < 0) putchar('-'),putch(-x);
31     else putch(x);
32 }
33 
34 void file(){
35     freopen("testdata(1).in","r",stdin);
36 //    freopen("1090.out","r",stdin);
37 }
38 
39 void readdata(){
40     read(n);
41     for(int i = 1;i <= n; ++ i){
42         read(a[i]);
43     }
44 }
45 
46 void work(){
47         l1 = 1,r1 = n + 1;
48     sort(a + 1,a + 1 + n);
49     l2 = r2 = 0;
50     for(int i = 1;i < n; ++ i){
51         long long x1,x2,x3,x4,x = 0;
52         if(l1 < r1) x1 = a[l1];
53         else x1 =1e15;
54         if(l1 < r1-1) x2 = a[l1 + 1];
55         else x2 = 1e15;
56         if(l2 < r2) x3 = q[l2];
57         else x3 =1e15;
58         if(x3 >X1) {
 59              X + = X1; L1 ++ ;
 60              IF (X3> X2) X + = X2, L1 ++ ;
 61 is              the else X + = X3, L2 ++ ;
 62 is          } the else {
 63 is              X + = X3; L2 ++ ;
 64              IF (L2 < R2) X4 = Q [l2]; // here l2 has ++ 
65              the else X4 = 1E15;
 66              IF (X4 <X1) X + = X4, l2 ++ ;
 67              the else X + = X1, L1 ++ ;
 68          }
 69          Q [ ++ R2] = X;
 70          ANS + = x;
71     }
72     put(ans);
73 }
74 
75 int main(){
76 //    file();
77     readdata();
78     work();
79     return 0;
80 }
View Code

 

Guess you like

Origin www.cnblogs.com/Mandy-H-Y/p/11419069.html