今天小马的第三题:dp 以及 deque 都可以做;我贴一下今天dp的垃圾code吧
if __name__ == '__main__':
s1 = input().split(' ')
N, K = int(s1[0]), int(s1[1])
s2 = input().split(' ')
As = [int(s) for s in s2]
dp_sum = [0] * N
dp_max = [0] * N
dp_min = [0] * N
dp_sum[K-1] = sum(As[:K])
dp_min[K-1] = min(As[:K])
dp_max[K-1] = max(As[:K])
ans = (dp_sum[K-1] - dp_min[K-1] - dp_max[K-1]) / (K-2)
for i in range(K, N):
dp_sum[i] = dp_sum[i-1] - As[i-K] + As[i]
if As[i] >= dp_max[i-1]:
dp_max[i] = As[i]
else:
if As[i] >= As[i-K]:
dp_max[i] = dp_max[i-1]
else:
if As[i-K] < dp_max[i-1]:
dp_max[i] = dp_max[i-1]
else:
dp_max[i] = max(As[i-K+1: i+1])
if As[i] <= dp_min[i-1]:
dp_min[i] = As[i]
else:
if As[i] <= As[i-K]:
dp_min[i] = dp_min[i-1]
else:
if As[i-K] > dp_min[i-1]:
dp_min[i] = dp_min[i-1]
else:
dp_min[i] = min(As[i-K+1: i+1])
v = (dp_sum[i] - dp_min[i] - dp_max[i]) / (K-2)
ans = max(ans, v)
print(ans)