1 回答
TA贡献1719条经验 获得超6个赞
您实施了一种蛮力方法来解决它。您的代码运行缓慢,时间复杂度 O(N^2),但您可以在 O(N*log(N)) 中执行此操作
而不是检查 ,对数组进行排序并使用 C 或 J 的最后一个结束时间活动进行检查。( 贪婪的方式解决它 )notOverlap(activity, arr)
在对数组进行排序之前,必须存储活动的索引。
这是我的解决方案,但在阅读解决方案之前,请尝试自己实现它
for testcasei in range(1, 1 + int(input())):
n = int(input())
acts = []
for index in range(n):
start, end = map(int, input().split())
acts.append((start, end, index)) # store also the index
acts.sort(reverse=True) # sort by starting time reversed
# so the first activity go to the last
d = ['']*n # construct the array for the answer
cnow = jnow = 0 # next time C or J are available
impossible = False # not impossible for now
while acts: # while there is an activity
start_time, end_time, index = acts.pop()
if cnow <= start_time: # C is available to do the activity
cnow = end_time
d[index] = 'C'
elif jnow <= start_time:
jnow = end_time
d[index] = 'J'
else: # both are'nt available
impossible = True
break
if impossible:
d = 'IMPOSSIBLE'
else:
d = ''.join(d) # convert the array to string
print("Case #%d: %s" % (testcasei, d))
我希望您找到这些信息,并帮助您理解并保持努力工作。
添加回答
举报