Ferry Loading II
memory limit: 65536KB time limit: 500MS
accept: 9 submit: 20
Description
Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?
Input
The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.
Output
For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time.
You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.
Sample Input
2
2 10 10
0
10
20
30
40
50
60
70
80
90
2 10 3
10
30
40
Sample Output
100 5
50 2
Source
ZQUCPC个人赛4
Author
Waterloo local
题意:
船渡车过河,每次渡n辆车,来回花2*t的时间,给出m辆车到达码头的时间,求出渡完所有车辆的最小时间。
提交次数:
2次,其中1次WA,1次AC
总耗时:
大约1小时
问题的分析与解题思路:
通过读测试数据,我发现为求得最小时间,除第一次外每次都渡n辆车,而第一次渡m%n辆车(即m除以n的余数),这样的时间总是最小的。对于这个结论我没做证明,但应该可以用数学归纳法证明。
错误做法:
比赛时我就想当然的用得出的结论做,直接求最后一辆车到达的时间,然后加上渡过河所用的时间作为总的最小时间,用m/n或者m/n+1(m%n不为0时)代替总的次数,WA了一次。这样求出的时间不一定是正确的,因为最后一次渡过的不一定有n辆车,而且每一辆车到达时不一定可以立即渡河,也有可能没车可以渡。
AC方法和数据结构:
应该根据前一辆车的情况推出下一辆车的情况,即由前一辆车渡过河后的总时间再来得到下一辆车渡过河后的总时间,所有的车的情况都要考虑。而且要注意前m%n辆车的情况的处理,因为他们是第一批过河的,其他的比较好处理。这应该用了贪心算法吧,我也不是很肯定。
AC代码:
算法复杂度分析:
时间复杂度是o(n)
算法的优缺点分析和改进:
其他解法:
1、动态规划法,因为是最优解问题,这题也可以用动态规划
总结:
1. 比赛时,要灵活处理,有些问题必须要证明自己的结论,有些问题则可以省略,但如果有时间就一定要证明(不过比赛时间一般不够)
2. 要熟练各种算法,不然运气好时做出了题也不肯定自己是不是用了某种算法,运气不好时抽象了模型也不知道用哪个算法。