OD E卷 - 实现【流浪地球】-解题代码

时间:2024-11-29 19:38:57

# 第一行输入N E
nums = [int(x) for x in input().split(" ")]
# 总发动机数
count = nums[0]
# 数组保存所有的节点,数组值,代表对应位置启动时间
engines = [-1 for i in range(count)]  # -1表示未激活

# 手动启动个数
start_cnt = nums[1]
min_time = float("inf")
for i in range(start_cnt):  # 行数
    nums1 = [int(x) for x in input().split(" ")]
    # 更新对应发动机的启动时间
    engines[nums1[1]] = nums1[0]
    # 当前时刻
    min_time = min(min_time, nums1[0])


def activate(index, time, count):
    # index当前已经启动的位置编号
    # time为当前时刻
    # count 为发动机总数
    global engines
    # 更新当前位置左右的节点
    left = index - 1
    if index == 0:
        left = count - 1
    right = index + 1
    if index == count - 1:
        right = 0

    # 激活
    if engines[left] == -1:
        engines[left] = time
    if engines[right] == -1:
        engines[right] = time


def find(start):  # start 为起始时间 0
    global engines
    flag = True
    while flag:
        for i in range(len(engines)):
            if engines[i] == start:  # 从开始位置向两边激活
                activate(i, start + 1, len(engines))

        start += 1
        active_cnt = 0  # 已激活的数量
        for i in range(len(engines)):
            if engines[i] != -1:
                active_cnt += 1

        # 全部激活,则停止
        if active_cnt == len(engines):
            flag = False

    # 最后激活的时间节点
    max_time = engines[0]
    for i in range(len(engines)):
        if engines[i] > max_time:
            max_time = engines[i]

    count = 0
    result = ""
    for i in range(len(engines)):
        if max_time == engines[i]:
            result += str(i) + " "
            count += 1

    # 输出最后激活的总数 及编号
    print(count)
    print(result[:-1])

find(min_time)