E题超时,求大佬指点。请问这样做复杂度是O(q n2)吗?
我是用前缀和先处理了一下A数组,然后对b数组的每个依次入堆,ans[i]存要i个b任务的最小时间,然后更新ans数组的值,能过83%,求大佬指点
import heapq n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = [float('inf')] * (n + 2) sa = [0] * (n + 2) sa[0] = a[0] q = [] for i in range(1, n): sa[i] = sa[i - 1] + a[i] for i in range(0, n): nt = sa[i] heapq.heappush(q, b[i]) res = 1 for j in heapq.nsmallest(i+1,q): nt += j ans[res] = min(ans[res], nt) res += 1 for i in range(m): t = int(input()) print(ans[t])