算法导论学习-子数组最大和问题

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 const int maxsize=101;
 5 int a[maxsize],sum[maxsize],n,inf=(1<<30);
 6 void solve(){
 7     if(n==0) return;
 8     int ans=-inf;
 9     sum[1]=a[1];
10     int left=1,right=1;
11     for(int i=2;i<=n;i++){
12         if(sum[i-1]>0){
13             sum[i]=sum[i-1]+a[i];
14             right++;
15         }
16         else{
17             sum[i]=a[i];
18             left=i;
19         }
20         if(sum[i]>ans) ans=sum[i];
21     }
22     printf("Subarray from indice %d->%d has the maximum sum: %d\n",left,right,ans);
23 }
24 int main(){
25     while(scanf("%d",&n)!=EOF){
26         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
27         solve();
28         /*
29             maxisum-subarray problem, in fact, is try to find subarray A[i...j]
30             of array A[1..i...j..n](1<=i<=j<=n). And the problem is meaingful
31             only when array A has both the positve value and negative elements.
32
33             considering the size of problem:
34             * if n==1, the problem is quite easy. A[1] is the right answer.
35             * if array A has more than one value, then we can use DP method to
36             solve it.
37
38             considering the procedure has already checked the A[i-1], and is
39             going to enter the i th check. then we can assume that we have
40             already get the maximum subarray of the A[1...i-1].supposing the
41             maxisum subarray of A[1..i-1] is A[p..i-1].
42
43             Then, our procedure is going to the ith check, we have aleady get
44             the solution of A[1..i], and we will move to check A[i]. so how the
45             relationship between sum[i-1] and sum[i]? From our experience,
46
47             * if sum[i-1]<0, then whatever the A[i] is, the sum of subarray
48             A[p...i-1,i] must be smaller than that of subarray A[p..i-1]. so we
49             modify the starting indice of maxisum from ‘p‘ to ‘i‘. And at the
50             same time, we can know that the maxisum subarray of A[1...i] is not
51             the A[p...i] but the A[p...i-1]. so the value of maxisum remains the
52             same: the sum of A[p...i].
53             * For another circumstance, if sum[i-1]>0, we can take two situations
54             int consideration, if A[i]>0 two, obviously, we should let
55             sum[i]=sum[i-1]+a[i]. then for A[i]<0? in fact, we also need to plus
56             A[i] onto sum[i-1]. as we know, in the (i-1)th calculation, acoording
57             to the above assumption, we have already found the solution to the
58             maxisum subarray: A[p...i-1]. So although the sum of A[p...i-1] is
59             higher than that of A[p...i], in fact, in each iteration, we compare
60             the value of sum[i-1] and sum[i] to the maxinum and store the maxinum.
61             So we let the sum[i-1]+A[i] has no matter to the maxinum.
62
63             (sum[i] stores the max sum of subarray end in A[i]).
64         */
65     }
66     return 0;
67 }
时间: 2024-08-07 08:27:37

算法导论学习-子数组最大和问题的相关文章

算法导论学习---红黑树具体解释之插入(C语言实现)

前面我们学习二叉搜索树的时候发如今一些情况下其高度不是非常均匀,甚至有时候会退化成一条长链,所以我们引用一些"平衡"的二叉搜索树.红黑树就是一种"平衡"的二叉搜索树,它通过在每一个结点附加颜色位和路径上的一些约束条件能够保证在最坏的情况下基本动态集合操作的时间复杂度为O(nlgn).以下会总结红黑树的性质,然后分析红黑树的插入操作,并给出一份完整代码. 先给出红黑树的结点定义: #define RED 1 #define BLACK 0 ///红黑树结点定义,与普通

【算法导论学习-015】数组中选择第i小元素(Selection in expected linear time)

1.算法思想 问题描述:从数组array中找出第i小的元素(要求array中没有重复元素的情况),这是个经典的"线性时间选择(Selection in expected linear time)"问题. 思路:算法导论215页9.2 Selection in expect linear time 2.java实现 思路:算法导论216页伪代码 /*期望为线性时间的选择算法,输入要求,array中没有重复的元素*/ public static int randomizedSelect(i

环状连续数组,求子数组最大和

今天看到环状连续数组求子数组最大和的题目,看了几篇博客,但是好像有问题,可以举出反例.于是参考其他人的博客自己又总结下. 首先,求非环状的数组中子数组 最大和问题是一个动态规划的思想. sum[i] = max(sum(i-1) + a[i], a[i]); sum[i]代表以i元素结尾的子数组的最大和,sum[i-1]代表以i-1元素结尾的子数组的最大和,a[i]代表第i个元素的值,由此公式可得,以第i个元素结尾的子数组的最大和可以由它之前的以第i-1个元素结尾的子数组的最大和推导出.如果以i

算法导论学习---红黑树详解之插入(C语言实现)

前面我们学习二叉搜索树的时候发现在一些情况下其高度不是很均匀,甚至有时候会退化成一条长链,所以我们引用一些"平衡"的二叉搜索树.红黑树就是一种"平衡"的二叉搜索树,它通过在每个结点附加颜色位和路径上的一些约束条件可以保证在最坏的情况下基本动态集合操作的时间复杂度为O(nlgn).下面会总结红黑树的性质,然后分析红黑树的插入操作,并给出一份完整代码. 先给出红黑树的结点定义: #define RED 1 #define BLACK 0 ///红黑树结点定义,与普通的二

算法导论 学习资源

学习的过程会遇到些问题,发现了一些比较好的资源,每章都会看下别人写的总结,自己太懒了,先记录下别人写的吧,呵呵. 1  Tanky Woo的,每次差不多都看他的 <算法导论>学习总结 - 1.前言 <算法导论>学习总结 - 2.第一章 && 第二章 && 第三章 <算法导论>学习总结 - 3.第四章 && 第五章 <算法导论>学习总结 - 4.第六章(1) 堆排序 <算法导论>学习总结 - 5.第六

【算法导论学习-014】计数排序(CountingSortTest)

参考:<算法导论>P194页 8.2节 Counting sort 1.Counting sort的条件 待排序数全部分布在0~k之间,且k是已知数:或者分布在min~max之间,等价于分布在0~max-min之间,max和min是已知数. 2.java 实现 /** * 创建时间:2014年8月17日 下午3:22:14 项目名称:Test * * @author Cao Yanfeng * @since JDK 1.6.0_21 类说明: 计数排序法,复杂度O(n), 条件:所有数分布在0

【算法导论学习-015】基数排序(Radix sort)

1.<算法导论>P197页 8.3节Radix sort 2.java实现 这里仅仅对[算法导论学习-014]计数排序 的参数进行了修改,同时仅仅修改了一行代码. /** * 创建时间:2014年8月17日 下午4:05:48 * 项目名称:Test * @author Cao Yanfeng * @since JDK 1.6.0_21 * 类说明: 利用计数排序实现基数排序 * 条件:待排序的所有数位数相同,注意,即便不相同,也可以认为是最多那个位数,如下面的例子可以认为都是3位数 */ p

【算法导论学习-016】两个已排过序的等长数组的中位数(median of two sorted arrays)

问题来源 <算法导论>P223 9.3-8: Let X[1..n] and Y[1..n] be two arrays, each containing nnumbers already in sorted order. Give an O(lgn)-time algorithm to find themedian of all 2n elements in arrays X and Y. 翻译过来即:求两个等长(n个元素)的已排序数组A和B的中位数 方案1:对两个数组进行归并直到统计到第n

【算法导论学习-012】n个数随机等概率的抽样m个

算法法导论>P129页课后题5.3-7 suppose we want to create a random sample of the set {1,2,3,-,n}, thatis, an m-element subset S, where0≤m≤n, such that each m-subset is equally likely to be created. One waywould be to set A[i]=i for i=1,2,3,-,n, call RANDOMIZE-IN