算法:时间分隔【贪婪】

时间分隔

Description

Given arrival and departure times of all trains that reach a railway station. Your task is to find the minimum number of platforms required for the railway station so that no train waits.

Note: Consider that all the trains arrive on the same day and leave on the same day. Also, arrival and departure times must not be same for a train.

Input

The first line of input contains T, the number of test cases. For each test case, first line will contain an integer N, the number of trains. Next two lines will consist of N space separated time intervals denoting arrival and departure times respectively.

Note: Time intervals are in the 24-hourformat(hhmm), preceding zeros are insignificant. 200 means 2:00.
Consider the example for better understanding of input.

Constraints:1 <= T <= 100,1 <= N <= 1000,1 <= A[i] < D[i] <= 2359

Output

For each test case, print the minimum number of platforms required for the trains to arrive and depart safely.

Sample Input 1

1
6
900 940 950 1100 1500 1800
910 1200 1120 1130 1900 2000

Sample Output 1

3

分析

要用最少的平台让火车安全到达和离开,即让一个平台尽可能多的安全停靠火车。
运用贪心算法,每次让一个平台尽可能多停靠火车,无法安全停靠所有火车时,再增加平台。

  1. 将火车按离开时间先后排序
  2. 定义一个lis数组,下标对应排序后火车序号,来标记该火车是否已经有平台停靠
  3. 将一个平台尽可能多的停靠列车:遍历火车数组,每次比较该火车到达时间和上一辆火车的离开时间
  4. 若所有火车遍历完,还有火车没有平台停,则递归增加一个平台(num+1)
  5. 直到所有火车均有平台停靠
# 时间分隔
# 描述
# 给定到达火车站的所有火车的到达和离开时间。您的任务是找到火车站所需的最少平台数量,以便没有火车等待。
# 注意:请考虑所有火车在同一天到达和离开同一天。另外,火车的到达和离开时间不得相同。
# 输入值
# 输入的第一行包含T,即测试用例的数量。对于每个测试用例,第一行将包含整数N,即列数。接下来的两行将由N个间隔的时间间隔组成,分别表示到达时间和离开时间。
# 注意:  时间间隔为24小时格式(hhmm),前面的零并不重要。200表示2:00。考虑该示例以更好地理解输入。
# 约束条件:1 <= T <= 100,1 <= N <= 1000,1 <= A [i] <D [i] <= 2359
# 输出量
# 对于每个测试用例,请打印火车安全到达和离开所需的最少平台数量。
# 样本输入1
# 1
# 6
# 900 940 950 1100 1500 1800
# 910 1200 1120 1130 1900 2000
# 样本输出1
# 3


def time_separate(arr, num, tem, lis):  # num是平台数量,tem是该平台最后一辆列车离开时间
    if min(lis) == 1:   # lis都为1,则所有列车均有平台可以停靠
        print(num)
        return
    for k in range(len(arr)):
        if lis[k] == 1:    # 该列车已经有平台停靠
            continue
        if arr[k][0] >= tem:    # 该列车可以停靠该平台
            tem = arr[k][1]
            lis[k] = 1
    if min(lis) == 1:   # 所有列车均有平台停靠
        print(num)
        return
    else:
        time_separate(arr, num + 1, 0, lis)


def takeSecond(elem):
    return elem[1]


if __name__ == '__main__':
    nums = int(input())
    for n in range(nums):
        N = int(input())
        begin = list(map(int, input().split()))
        end = list(map(int, input().split()))
        arr = []
        for i in range(N):
            arr.append([begin[i], end[i]])
        arr.sort(key=takeSecond)    # 按到达时间先后排序
        lis = [0 for x in range(N)]
        time_separate(arr, 1, 0, lis)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值