from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf from time import perf_counter as pfc def in_between(z: AlgoDatArray, l: int, m: int, r: int) -> (AlgoDatValue, int, int): left_max = AlgoDatValue(MinusInf()) left = AlgoDatValue(0) current_sum = AlgoDatValue(0) for i in range(m, l - 1, -1): current_sum += z[i] if current_sum > left_max: left_max.value = current_sum.value left.value = i right_max = AlgoDatValue(MinusInf()) right = AlgoDatValue(0) current_sum.value = 0 for i in range(m + 1, r + 1): current_sum += z[i] if current_sum > right_max: right_max.value = current_sum.value right.value = i return left_max + right_max, left, right def maxfolge_part(z: AlgoDatArray, l:int, r:int) -> (int, int, int): if l == r: return z[l].value, l, r m = (l + r) // 2 left_sum, left_left, left_right = maxfolge_part(z, l, m) right_sum, right_left, right_right = maxfolge_part(z, m + 1, r) cross_sum, cross_left, cross_right = in_between(z, l, m, r) if left_sum >= right_sum and left_sum >= cross_sum: return left_sum, left_left, left_right elif right_sum >= left_sum and right_sum >= cross_sum: return right_sum, right_left, right_right else: return cross_sum, cross_left, cross_right def maxfolge3(z: AlgoDatArray) -> (int, int, int): return maxfolge_part(z, 0, z.size - 1) if __name__ == "__main__": z = read_int_sequence("../../seq3.txt") start = pfc() print(maxfolge3(z)) print(f"Dauer: {pfc() - start:.4f}s") AlgoDatValue.summary()