当前位置:网站首页>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;
}
边栏推荐
- PR FAQ, what about PR preview video card?
- What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
- Unique line of "Gelu"
- Realization of mask recognition based on OpenCV
- Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
- Missing number
- 130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
- Returns the size of the largest binary search subtree in a binary tree
- Explain in detail the process of realizing Chinese text classification by CNN
- 带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
猜你喜欢
Digital twin smart factory develops digital twin factory solutions
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
Many to one, one to many processing
容器运行时分析
Convolution和Batch normalization的融合
67 page overall planning and construction plan for a new smart city (download attached)
sysdig分析容器系统调用
Practical series - free commercial video material library
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
随机推荐
Custom throttling function six steps to deal with complex requirements
Returns the root node of the largest binary search subtree in a binary tree
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
[reading notes] phased summary of writing reading notes
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
How much do you know about synchronized?
Judge whether the binary tree is full binary tree
返回二叉树两个节点间的最大距离
leetcode 650. 2 keys keyboard with only two keys (medium)
Realization of mask recognition based on OpenCV
Hit the industry directly! The propeller launched the industry's first model selection tool
Linux 下安装 redis
cocospods 的使用
【ML】李宏毅三:梯度下降&分类(高斯分布)
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
哪些软件可以整篇翻译英文论文?
zhvoice
Leetcode relaxation question - day of the week
论文的设计方案咋写?