基本思路:
将所有的时间节点(借钥匙时间,还钥匙时间)组织到一个列表里【钥匙号,时间,借or还】
并将之按时间的升序、还优先借、钥匙号的升序排列
那么所要做的就是遍历这个列表:
····如果是借就
········将要钥匙盒对应的位置置零
····如果是还就
········遍历钥匙盒
············找到第一个零,将之变成钥匙号
# 201709-2 公共钥匙盒
N, K = map(int, input().split())
keybox = []
com = []
all = []
for i in range(N):
keybox.append(i + 1)
for i in range(K):
com.append(list(map(int, input().split())))
all.append([com[i][0], com[i][1], -1])
all.append([com[i][0], com[i][1] + com[i][2], 1])
all.sort(key=lambda x: (x[1],-x[2],x[0]))
for i in range(len(all)):
if all[i][2] == -1: # borrow
keybox[keybox.index(all[i][0])] = 0
elif all[i][2] == 1: # return
for j in range(len(keybox)):
if keybox[j] == 0:
keybox[j] = all[i][0]
break
for i in range(len(keybox)):
print(keybox[i], end=' ')