码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-01-06 02:00:07      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

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

标签:

原文地址:http://www.cnblogs.com/fu11211129/p/4204951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!