当前位置:网站首页>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;
}
边栏推荐
- 请求与响应
- Optimization of streaming media technology
- Integration of revolution and batch normalization
- CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
- The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
- Leetcode relaxation question - day of the week
- Analyze ad654: Marketing Analytics
- Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
- AcWing_188. 武士风度的牛_bfs
- JDBC教程
猜你喜欢

直击产业落地!飞桨重磅推出业界首个模型选型工具

Bean load control

95页智慧教育解决方案2022

35页危化品安全管理平台解决方案2022版

Realization of mask recognition based on OpenCV
![[shutter] open the third-party shutter project](/img/1a/e35d0180612d7e79b55e7818193740.jpg)
[shutter] open the third-party shutter project

Detailed explanation of 'viewpager' in compose | developer said · dtalk

秒杀系统设计
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

Pytorch里面多任务Loss是加起来还是分别backward?
随机推荐
[array] binary search
Chapter 3 of getting started with MySQL: database creation and operation
[shutter] shutter open source project reference
JSON数据传递参数
Develop knowledge points
返回二叉树中最大的二叉搜索子树的根节点
JDBC教程
顶级 DevOps 工具链大盘点
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
Interface automation coverage statistics - used by Jacobo
Digital collection trading website domestic digital collection trading platform
MySQL Foundation
请问大家在什么网站上能查到英文文献?
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
接口差异测试——Diffy工具
Request and response
Why can't the start method be called repeatedly? But the run method can?
JDBC tutorial
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)