if __name__ == "__main__": (x,y,z) = map(str , input().split()) number = int(input()) counter = 0 time_list = [] red_time = int("1" + x) yellow_time = int("2"+y) green_time = int("3"+z) for i in range(int(x)): time_list.append(red_time-i) for i in range(int(z)): time_list.append(green_time - i) for i in range(int(y)): time_list.append(yellow_time - i) def re_index(flag_count , time_list , counter): try:index = time_list.index(flag_count) except:return -1 inter_index = index+1+counter-len(time_list) if inter_index > 0: index = inter_index - 1 else: index = index+counter return index for i in range(number): (n,m) = map(str , input().split()) if "2" not in n and len(m) < 2: n = n+"0" flag_count = int(n+m) index = re_index(flag_count=flag_count , time_list = time_list , counter = counter) if index == -1: counter = counter+flag_count else: true_time = str(time_list[index]) if "1" in true_time[0]: true_time = true_time.replace("1" , "0" , 1) counter = counter + int(true_time) if "2" in true_time[0]: true_time = true_time.replace("2" , "0" , 1) counter = counter + int(true_time)+int(x) print(counter)
将红绿灯的持续时间和状态标志合在一起记为一个整数,然后依次放入数组中,红,绿,黄为30,3,30时,数组为[130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 330, 329, 328, 327, 326, 325, 324, 323, 322, 321, 320, 319, 318, 317, 316, 315, 314, 313, 312, 311, 310, 309, 308, 307, 306, 305, 304, 303, 302, 301, 23, 22, 21]。
然后结合已经用去的时间来判断此刻红绿灯的状态,然后在计算在路口花费的时间,但是超时了,我以为是后面比较的问题,然后我尝试新添加一个直接判断特定状态所需时间的数组,[30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 32, 31],不用再if比较,但还是超时了,看来这个方法用python行不通,但也是一个思路