当前位置:网站首页>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;
}
边栏推荐
- Installing redis under Linux
- 接口差异测试——Diffy工具
- CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
- [shutter] shutter open source project reference
- PR FAQ, what about PR preview video card?
- 教育学大佬是怎么找外文参考文献的?
- leetcode 650. 2 keys keyboard with only two keys (medium)
- Explain in detail the process of realizing Chinese text classification by CNN
- leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
- Xcode real machine debugging
猜你喜欢

How QT exports data to PDF files (qpdfwriter User Guide)

Intranet penetration | teach you how to conduct intranet penetration hand in hand

CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)

67页新型智慧城市整体规划建设方案(附下载)

67 page overall planning and construction plan for a new smart city (download attached)

Mapper agent development
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)

带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)

JDBC tutorial
随机推荐
Container runtime analysis
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
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
Explain in detail the process of realizing Chinese text classification by CNN
67 page overall planning and construction plan for a new smart city (download attached)
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
接口差异测试——Diffy工具
Bean load control
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
Why can't the start method be called repeatedly? But the run method can?
MATLAB signal processing [Q & a notes-1]
容器运行时分析
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
Leetcode skimming - game 280
Digital twin smart factory develops digital twin factory solutions
Top Devops tool chain inventory
Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
leetcode 650. 2 keys keyboard with only two keys (medium)
Many to one, one to many processing
实用系列丨免费可商用视频素材库