真费劲...,很详细
# 100个座位,每5个一排
# 一个人可购最多不超过5张票
# 安排在编号最小的相邻座位, 否则应该安排在编号最小的几个空座位
def find_small_adj(o, seats):
# 从小编号开始找连续o个位置是空位,且不能跨排的情况
count = 0
serie = []
isSpan = False
for i in range(len(seats)):
if count != o:
if seats[i] == 0:
count += 1
serie.append(i)
else:
count = 0
serie.clear()
if count == o: # 找到连续o个位置是空位
isFind = True
for s in serie:
if s%5==4 and serie.index(s)!=o-1: # 判断连续的座位是否跨排了:每一排最后一个元素处在serie中间位置则跨排
isSpan = True
isFind = False
count = 0
tmp = serie[serie.index(s)+1:] # 去除跨排前的元素,保留下一排的元素
serie.clear()
serie=tmp
count = len(serie)
isSpan = False # 这里注意,不然就会错误
break
if isFind == True: # 已经找到,不必再往下找,直接跳出seats的遍历
break
if isSpan == False and len(serie)==o: # 找到连续o个位置是空的,且没有跨排
return serie
else: # 没有找到连续o个位置是空的且不跨排,那就安排在编号最小的几个空位上,不考虑是否空
serie.clear()
count = 0
for i in range(len(seats)):
if count != o:
if seats[i] == 0:
count += 1
serie.append(i)
if count == o:
break
return serie
seats = [0]*100
n = int(input())
orders = list(map(int, input().split()))
for o in orders:
res = find_small_adj(o, seats)
for r in res:
seats[r] = 1
for re in res:
print(re+1,end=' ')
print()
|