标签:spl for 大连 rate == split() 最大 转移 __name__
以HDU1231为例,代码之没法交如下:
inf = 0x3f3f3f3f a = [0 for i in range(10005)] ans, L, R = -inf, 0, 0 def divide_and_conquer(l, r): global ans, L, R if l > r: return if l == r: if ans < a[l]: ans = a[l] L, R = l, r elif ans == a[l] and l < L: L, R = l, r return mid = (l + r) // 2 divide_and_conquer(l, mid-1) divide_and_conquer(mid+1, r) tmp = a[mid] l_ans, r_ans = 0, 0 # 左、右边部分的最大连续和(不含a[mid]) lL, rR = mid, mid ll, rr = mid-1, mid+1 l_tmp, r_tmp = a[ll], a[rr] while ll >= l and tmp+l_tmp > 0: if l_ans < l_tmp: l_ans = l_tmp lL = ll ll -= 1 l_tmp += a[ll] while rr <= r and tmp+r_tmp > 0: if r_ans < r_tmp: r_ans = r_tmp rR = rr rr += 1 r_tmp += a[rr] if ans < tmp + l_ans + r_ans: ans = tmp + l_ans + r_ans L, R = lL, rR elif ans == tmp + l_ans + r_ans and lL < L: L, R = lL, rR if __name__ == ‘__main__‘: k = int(input()) for i, e in enumerate(list(map(lambda x: int(x), input().split()))): a[i+1] = e divide_and_conquer(1, k) if ans < 0: L, R = 1, k print(ans, a[L], a[R])
DP方法就较为简单了,状态dp[i]表示以第i个元素作为结尾的连续子序列的最大和,转移:遍历时,若前一个状态小于0则dp[i] = e[i],否则dp[i] = dp[i-1] + e[i]。
go on~
divide and conquer - 最大连续子序列 - py
标签:spl for 大连 rate == split() 最大 转移 __name__
原文地址:https://www.cnblogs.com/boceng/p/11502708.html