先用 accumulate 累積,
再用 bisect 去追每個值。
cpp 應該能用 lower bound 去完成每個任務。別一個一個數。
您好,請問我這Python code只有20分,其餘TLE,有辦法再優化時間嗎? 謝謝。
[n,m]=[int(x) for x in input().split()]
p=[int(x) for x in input().split()]
pp=p+p
q=[int(x) for x in input().split()]
spp=[0]*(2*n)
spp[0]=pp[0]
for i in range(1,len(pp)):
spp[i]=spp[i-1]+pp[i]
a=0 # a is house index as one mission finished, initially at 0
for i in range(len(q)):
L=a # left index at initial
R=a+n-1 # right index at initial
while L<R:
m=int((L+R)/2)
if q[i]>spp[m]-spp[a]+pp[a]:
L=m+1
else: R=m
a=(L+1)%n # L=R
print(a)