最长子序列和(分治法实现)

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<cmath>
 7 #include<stack>
 8 #include<set>
 9 #include<queue>
10 #include<algorithm>
11 using namespace std;
12 const long long mod=1000000007;
13 const long long min_=-1000000000;
14 int maxsub(const int a[],int left,int right){
15        int maxleftsum,maxrightsum;
16        int maxleftbordersum,maxrightbordersum;
17        int leftbordersum,rightbordersum;
18        int center,i;
19        if(left==right)
20            if(a[left]>0)
21            return a[left];
22        else return 0;
23        center=(left+right)/2;
24        maxleftsum=maxsub(a,left,center);
25        maxrightsum=maxsub(a,center+1,right);
26        maxleftbordersum=leftbordersum=0;
27        for(i=center;i>=left;i--)
28        {
29            leftbordersum+=a[i];
30            if(leftbordersum>maxleftbordersum)
31                maxleftbordersum=leftbordersum;
32        }
33        maxrightbordersum=rightbordersum=0;
34        for(i=center+1;i<=right;i++)
35        {
36            rightbordersum+=a[i];
37            if(rightbordersum>maxrightbordersum)
38             maxrightbordersum=rightbordersum;
39        }
40        int t=max(maxleftsum,maxrightsum);
41        return max(t,maxleftbordersum+maxrightbordersum);
42 }
43 int main()
44 {
45     int a[55];
46     int n;
47     cin>>n;
48     int sum=0;
49     for(int i=0;i<n;i++){
50         cin>>a[i];
51         sum+=a[i];
52     }
53         cout<<maxsub(a,0,n-1)<<" "<<sum<<endl;
54     return 0;
55 }
View Code

最长子序列和,产生情况,用分治法就是左边或者右边,或者横跨俩边,

建立base case就是left=right则返回a[left]的值。

分治关键在于后面的和,俩边可以递归实现。

posted @ 2017-10-15 19:17  余生漫漫浪  阅读(503)  评论(0编辑  收藏  举报