循环数组的最大子段和


转自:http://blog.csdn.net/acdreamers/article/details/38760805



题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050

 

题意:给定一个长度为50000的数组,求它的循环数组的最大子段和。

 

分析:本题与普通的最大子段和问题不同的是,最大子段和可以是首尾相接的情况,即可以循环。那么这个题目的最

     大子段和有两种情况

 

    (1)正常数组中间的某一段和最大。这个可以通过普通的最大子段和问题求出。

    (2)此数组首尾相接的某一段和最大。这种情况是由于数组中间某段和为负值,且绝对值很大导致的,那么我

        们只需要把中间的和为负值且绝对值最大的这一段序列求出,用总的和减去它就行了。

 

     即,先对原数组求最大子段和,得到ans1,然后把数组中所有元素符号取反,再求最大子段和,得到ans2,

     原数组的所有元素和为ans,那么最终答案就是 max(ans1, ans + ans2)

 

代码:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4.   
  5. using namespace std;  
  6. typedef long long LL;  
  7. const int N = 50005;  
  8.   
  9. int a[N];  
  10.   
  11. LL Work(int a[], int n)  
  12. {  
  13.     LL ans = 0;  
  14.     LL tmp = 0;  
  15.     for(int i=0; i<n; i++)  
  16.     {  
  17.         if(tmp < 0) tmp = a[i];  
  18.         else tmp += a[i];  
  19.         ans = max(ans, tmp);  
  20.     }  
  21.     return ans;  
  22. }  
  23.   
  24. int main()  
  25. {  
  26.     int n;  
  27.     while(scanf("%d", &n)!=EOF)  
  28.     {  
  29.         LL ans = 0;  
  30.         for(int i=0; i<n; i++)  
  31.         {  
  32.             scanf("%d", &a[i]);  
  33.             ans += a[i];  
  34.         }  
  35.         LL ans1 = Work(a, n);  
  36.         for(int i=0; i<n; i++)  
  37.             a[i] = -a[i];  
  38.         LL ans2 = Work(a, n);  
  39.         ans = max(ans + ans2, ans1);  
  40.         printf("%I64d\n", ans);  
  41.     }  
  42.     return 0;  
  43. }  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值