## 贪心

贪心

##打卡第 2*3 天
一、贪心算法的理解(个人观点仅供参考)
在当前的情况中选择一个最优解,这个最优解不一定是整体的最优解,也有可能是个近似的最优解(看题目类型)。我们做出的决策只是当前看来是最好的,至于之后是否有更好的决策不得而知,但是只要保证每一都步是最优解,就能得到题目整体的最优解,有时可能是个近似最优解。

二、例题
Due to the development of online shopping, the logistics industry which is highly connectedwith goods shipping has been so prosperous that the great amount of employees is needed.Therefore, Alex, a truck driver in this growing industry, was supposed to transport severalparcels scattering in the warehouse to other cities in his daily routine.

According to the official safety requirements to the trucks running in the highway, Alex hadto tie up all the packages tightly so that he could settle the goods safely on his truck. Alexknew that the length of the cords needed for bundling the packages on the truck was based on the size of the packages themselves. Also,
n packages can be tied up well after n−1 bundles.Moreover, when bundling goods, Alex could only bundle two packages at one time to avoidscattering. Since the daily consumption of the cord was great and Alex was supposed to payfor it, he hopes to bundle all the goods with the shortest cord.

For example, there are 4 parcels in the size of 8, 5, 14, and 26 respectively. If Alex binds thefirst two together, the needed rope will be in the length of 13 (8+5 = 13) while the needed ropefor the latter two packages will be 40 (14 + 26 = 40). If Alex keeps bundling these two items,the rope length he needs will be 53 (13 + 40 = 53). So the total length of the 4 packages willbe 106 (13 + 40 + 53 = 106). If Alex tries another way by bundling the first two (8 + 5 = 13),adding up the third one (13 + 14 = 27), and then bundling the last item (27 + 14 = 53), he willonly need the cord in the length of 93 (13 + 27 + 53 = 93). Now your mission is to help Alexfinding the minimum length of the needed cord.

Input Format
The first line contains an integer T
indicating the number of test cases. Each test case contains two lines. The first one contains a positive integer n indicating the number of packages. The second one contains n positive integers separated by a space to indicate the size of each parcel.

Output Format
The first line contains an integer T indicating the number of test cases. Each test case contains two lines. The first one contains a positive integer n indicating the number of packages. The second one contains n positive integers separated by a space to indicate the size of each parcel.

Technical Specification
1≤T≤10
1≤n≤1000
The size of each parcel is at most 1000
.
Sample Input
4
6
2 3 4 4 5 7
5
5 15 40 30 10
10
3 1 5 4 8 2 6 1 1 2
9
3 2 1 6 5 2 6 4 3

Sample Output
63
205
100
98

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值