divide and conquer - 最大连续子序列 - py

Posted boceng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了divide and conquer - 最大连续子序列 - py相关的知识,希望对你有一定的参考价值。

以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的主要内容,如果未能解决你的问题,请参考以下文章

Divide and conquer:Sumsets(POJ 2549)

LeetCode分治法 divide and conquer (共17题)

Divide and Conquer-169. Majority Element

[leetcode]Divide and Conquer-169. Majority Element

Divide and Conquer53.Maximum Subarray(easy)

分治(Divide and Conquer)算法之归并排序