ACM竞赛学习记录------Crossing River(贪心算法)

这篇博客记录了ACM竞赛中的一道问题,即如何使用一艘最多载两人且速度受慢者限制的船只,让N个人以最短时间过河。博主详细解析了解题思路,提出了贪心算法,包括当人数小于等于2、3以及大于3时的最优策略,并给出了AC代码实现。
摘要由CSDN通过智能技术生成

题目链接:https://vjudge.net/contest/370495#problem/F
题目:
A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won’t be more than 1000 people and nobody takes more than 100 seconds to cross.
Output
For each test case, print a line containing the total number of seconds required for all the N people to cross the river.
Sample Input

1
4
1 2 5 10

Sample Output

17

题目大意:
有N个人想过河,但只有一艘船,船只能载两个人,每个人都有自己的过河速度,但如果两个人一起过和则过河速度取决于速度较慢的那一个人,问人全都过河所需要的最短时间。
解题思路(贪心):
我们需要分情况:
1.若需要过河的人数小于等于两个,则过河最短时间就是速度较慢的那个人。
2.若与需要过河的人数为三人,则每次过河都是由速度最快的那人带着速度较慢的一人过河,然后由速度最快的那人划船回来,再接下一个,这样的运输方法最快。
3.若需要过河的人数大于三人,由于速度较快的人是优秀的工具人,我们要多次利用,所以我们每次运两个最慢的人过去,一共有两种最优运法:
①:用速度最快的人分别“护送”两个最慢的人过河,最后都由速度最快的将船开回。
②:先将速度最快和第二快的人组队,送第二快的到对面,再将速度最慢和第二慢的两个组队送到对面,再由第二快的将船开回。
两种方法效果完全相同,都将速度最慢的送到了对面,时间长短不同,两种方法中取最短的就可,最后送完所有人得到的时间累加就是最短时间。
(贪心:部分和最优就是整体和最优)
AC代码:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值