我们的货物一共有重量和体积两种属性,看题目不难看出,至少要有一种属性达到标准才能被无人机搬走,那最简单的结果就出来了:当一件货物的重量和体积都大的离谱的时候,就没有无人机能把它搬动,此时输出的就是-1,反之就一定能有结果。那么,我们将货物进行分类,分成只能被一种无人机搬走和能被两种无人机搬走共计三类。之后只要优先搬走只能被一种无人机搬走的货物,并计算时间就可以了。
代码:
import bisect
from collections import deque
def min_time(a, b, T, x, y, goods):
x.sort()
y.sort()
weight = deque() # 仅重量限制能处理
volume = deque() # 仅体积限制能处理
both = deque() # 两者都能处理
for w, s in goods:
weight_ok = bisect.bisect_right(x, w) >=0 and bisect.bisect_right(x, w)<len(x)
volume_ok = bisect.bisect_right(y, s) >=0 and bisect.bisect_right(y, s)<len(y)
if weight_ok and volume_ok:
both.append((w, s))
elif weight_ok:
weight.append((w, s))
elif volume_ok:
volume.append((w, s))
else:
return -1
time = 0
while weight or volume or both:
for i in range(a):
if weight:
weight.popleft()
elif both:
both.popleft()
for j in range(b):
if volume:
volume.popleft()
elif both:
both.popleft()
time += 1
if not weight and not volume and not both:
break
return time if not weight and not volume and not both else -1
with open("in.txt", "r") as file:
lines = file.readlines()
a,b,t = map(int, lines[0].split())
x= list(map(int, lines[1].split()))
y= list(map(int, lines[2].split()))
goods = [tuple(map(int, line.split())) for line in lines[3:]]
result = min_time(a,b,t,x,y, goods)
print(result)
with open('out.txt', 'w') as f:
f.write(str(result))