当前位置:网站首页>Question e: merged fruit -noip2004tgt2
Question e: merged fruit -noip2004tgt2
2022-07-03 00:08:00 【Wait for dawn forever】
Title Description
In an orchard , Dodo has beaten all the fruit down , And according to different kinds of fruits, they are divided into different heaps . Duoduo decides to combine all the fruits into a pile . Every merger , Duoduo can combine the two piles of fruit , The energy consumed is equal to the sum of the weight of two heaps of fruits . It can be seen that , All the fruits pass by n-1 After the merger , There's only one pile left . Duoduo's total energy consumed during fruit merging is equal to the sum of energy consumed during each merging .
Because I have to work hard to carry these fruits home , Therefore, Duoduo should save energy as much as possible when merging fruits . Suppose that each fruit weighs 1, And we know the number of kinds of fruit and the number of each kind of fruit , Your task is to design a sequence plan for the merger , To minimize the amount of physical effort expended , And output this minimum energy cost .
For example, there are 3 Plant fruit , The order of numbers is 1,2,9. You can put 1、2 Heap merge , The number of new piles is 3, Expend physical energy for 3. next , Merge the new pile with the original third pile , And get a new pile , The number is 12, Expend physical energy for 12.
So a lot of energy =3+12=15. Can prove that 15 For the minimum physical cost .
Input
The input consists of two lines , The first line is an integer n(1<=n<=10000), The number of kinds of fruit .
The second line contains n It's an integer , Separate... With spaces , The first i It's an integer ai(1<=ai<=20000) It's No i The number of fruits planted .
Output
The output includes a line , This line contains only one integer , That's the minimum physical cost . Input data to ensure that the value is less than 2^31.
The sample input
3
1 2 9
Sample output
15
Ideas : Priority queue .
#include <cstdio>
#include <queue>
using namespace std;
// Priority queue representing small top heap
priority_queue<int, vector<int>, greater<int>> q;
int main() {
int n, temp, x, y;
while (scanf("%d", &n) != EOF) {
int sum = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &temp);
q.push(temp);
}
while (q.size() > 1) {
x = q.top();
q.pop();
y = q.top();
q.pop();
q.push(x + y); // The sum of the two is added to the priority queue as a new number
sum += x + y;
}
q.pop();
printf("%d\n", sum);
}
return 0;
}
边栏推荐
猜你喜欢
Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
Architecture: load balancing
Matlab 信号处理【问答笔记-1】
Convolution和Batch normalization的融合
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
随机推荐
Codeforces Round #771 (Div. 2)---A-D
Interface switching based on pyqt5 toolbar button -2
Architecture: database architecture design
带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
Judge whether the binary tree is full binary tree
In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
JDBC練習案例
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
sysdig分析容器系统调用
MFC 获取当前时间
ArrayList analysis 2: pits in ITR, listiterator, and sublist
Architecture: load balancing
Unique line of "Gelu"
程序分析与优化 - 9 附录 XLA的缓冲区指派
[shutter] shutter open source project reference
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
S12. Verify multi host SSH mutual access script based on key
判断二叉树是否为满二叉树
[OJ] intersection of two arrays (set, hash mapping...)