From a69f9b3f61163215d6a8d0abd254f1d938449d05 Mon Sep 17 00:00:00 2001 From: Fulong Ma Date: Sat, 11 Nov 2023 22:28:54 +0800 Subject: [PATCH] =?UTF-8?q?update=20+=5Fposts/Tech/algorithms/2018-03-13-?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E4=B9=8B=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E5=AD=90=E5=BA=8F=E5=88=97=E5=92=8C=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E7=A7=AF=E5=8F=8A=E9=80=92=E5=A2=9E=E5=BA=8F?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\242\236\345\272\217\345\210\227.md" | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git "a/_posts/Tech/algorithms/2018-03-13-\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\345\272\217\345\210\227\345\222\214\345\255\220\345\272\217\345\210\227\347\247\257\345\217\212\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/_posts/Tech/algorithms/2018-03-13-\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\345\272\217\345\210\227\345\222\214\345\255\220\345\272\217\345\210\227\347\247\257\345\217\212\351\200\222\345\242\236\345\272\217\345\210\227.md" index 798aba877e..3434a99bc9 100644 --- "a/_posts/Tech/algorithms/2018-03-13-\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\345\272\217\345\210\227\345\222\214\345\255\220\345\272\217\345\210\227\347\247\257\345\217\212\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/_posts/Tech/algorithms/2018-03-13-\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\234\200\345\244\247\350\277\236\347\273\255\345\255\220\345\272\217\345\210\227\345\222\214\345\255\220\345\272\217\345\210\227\347\247\257\345\217\212\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -32,7 +32,27 @@ int MaxSum(int n) return sum; } ``` -C++: +Python + +```python +def max_sum(n, a): + sum_val = 0 + b = 0 + for i in range(n): + if b > 0: + b += a[i] + else: + b = a[i] + if b > sum_val: + sum_val = b + return sum_val + +``` + + + +C++: 找最大的对应的左右区间。 + ```c++ #include #include @@ -143,6 +163,8 @@ class Solution: ``` +## 动态规划之最长递增子序列 + 最长非递减子序列 ```python \# 最长非递减子序列 @@ -157,4 +179,52 @@ def LIS(self, nums: List[int]): g[j] = x return len(nums) - len(g) -``` \ No newline at end of file +``` + +## 最大非递减子序列和 + +- [2926. 平衡子序列的最大和](https://mafulong.eu.org/2023/11/11/2926.-%E5%B9%B3%E8%A1%A1%E5%AD%90%E5%BA%8F%E5%88%97%E7%9A%84%E6%9C%80%E5%A4%A7%E5%92%8C/) + + + +O(nlogn) + +线段树维护前面的最大子序列和。每次找小于当前元素的最大子序列和。 + +```python +MOD = int(1e9 + 7) +INF = float('inf') +import sortedcontainers +import bisect +import heapq + +class Solution: + def maxBalancedSubsequenceSum(self, nums: List[int]) -> int: + # len = 12 + d = [] + n = len(nums) + l = INF + r = float('-inf') + for i in range(n): + if nums[i] > 0: + # l = min(l, nums[i] - i) + # r = max(r, nums[i] - i) + d.append((nums[i] - i, nums[i])) + if not d: + return max(nums) + seg = SegmentTree(SegOpCollect.Collect_Max, SegOpUpdate.Update_SetVal, default_val=0) + diff2index = {} + x = sorted(set([v[0] for v in d])) + for v in x: + diff2index[v] = len(diff2index) + # print(d) + # print(diff2index) + l, r = 0, len(diff2index) + # len = 12 + seg.build(left=l, right=r) + for v in d: + t = seg.query_interval(l, diff2index[v[0]]) + seg.set_point(diff2index[v[0]], t + v[1]) + return seg.query_interval(l, r) +``` +