循环数组求最大子段和系列-----------方法1

来源:互联网 发布:生理学软件 编辑:程序博客网 时间:2024/05/16 19:28
EquatorTime Limit: 2000ms, Special Time Limit:5000ms,Memory Limit:65536KBTotal submit users: 12, Accepted users:8Problem 13248 : No special judgementProblem description

In a galaxy far away, the planet Equator is under attack! The evil gang Galatic Criminal People Cooperation is planning robberies in Equator’s cities. Your help is needed! In order to complete your training for becoming a lord of the dark side you should help them deciding which cities to rob. As the name says, the desert planet Equator only can be inhabited on its equator. So the gang lands there at some point and travels into some direction robbing all cities on their way until leaving the planet again.

But what is still open for them is to decide where to land, which direction to take, and when to leave. Maybe they shouldn’t even enter the planet at all? They do not consider costs for traveling or for running their ship, those are peanuts compared to the money made by robbery!

The cities differ in value: some are richer, some are poorer, some have better safety functions. So the gang assigned expected profits or losses to the cities. Help them deciding where to begin and where to end their robbery to maximize the money in total when robbing every city in between.

Input

Output

For each test case print one integer describing the maximum money they can make in total.

Sample Input
33 1 2 38 4 5 -1 -1 1 -1 -1 52 -1 -1
Sample Output
6140


      该题就是典型的求循环数组中最大子段和的问题!当然要暴力解决就可以直接写两个for循环,不过当然也肯定过不了再见,超时!!所以就需要想一下其他方法。在专题第一讲中我们知道了怎么求普通数组中最大子段和的问题,我们想一下是不是循环数组可以和普通数组挂钩呢??

      其实循环数组中最大值也可能是普通数组中的最大值,还有可能是数组首尾相连后的部分出现最大值。那么第一种情况我们当然会解决,但是后者呢??

      可以想一下为什么会出现后者这种情况。不难想到,之所以会出现后者是因为普通数组中有一段连续的和为负数,并且绝对值比较大,以至于影响到普通数组首尾相连后最大子段数发生改变。所以我们只需要把原来数组中的数全部取相反数再由普通数组方法求出最大子段数的和就是原来数组中连续负数的绝对值max2;当然一开始要求出普通数组中最大子段和max1,和总和sum。比较max1和sum+max2,就可以得到答案。

      代码如下:

<span style="font-size:18px;">#include <iostream>#include <algorithm>#include <cstdio>using namespace std;#define max(a,b)  ((a)>(b)?(a):(b))#define MAXN 1000005int a[MAXN];int max_sum(int a[],int n);int main(){   int n,i,t;    cin>>t;    while(t--)    {  int sum=0;       cin>>n;       for(i=0;i<n;i++)       {   cin>>a[i];           sum+=a[i];       }       int max1=max_sum(a,n);       for(i=0;i<n;i++)           a[i]=-a[i];       int max2=max_sum(a,n);       cout<<max(max1,sum+max2)<<endl;    }}int max_sum(int a[],int n){   int sub_array=0;    int i,sum=0;    for(i=0;i<n;i++)    {   if(sum>=0)            sum+=a[i];        else            sum=a[i];        if(sub_array<sum)            sub_array=sum;    }    return sub_array;}</span>


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿吃多了吐奶怎么办 20个月孩子便秘怎么办 一岁宝宝肛裂怎么办 婴儿吃饱了吐奶怎么办 23天新生儿吐奶怎么办 婴儿吐奶舌苔白怎么办 宝宝吐奶酸臭味怎么办? 1周岁吐奶有酸味怎么办 十多天的宝宝吐奶怎么办 未满月婴儿吐奶怎么办 2个月宝宝溢奶怎么办 四岁宝宝说话结巴怎么办 小孩说话结巴打顿怎么办 2岁宝宝突然说话结巴怎么办 2岁宝宝突然结巴怎么办 幼儿舌头起泡牙龈出血怎么办 小孩长得太快怎么办 脑出血压着神经不会说话怎么办 四岁宝宝说话有点口吃怎么办 三岁宝宝有点口吃怎么办 3岁宝宝有点口吃怎么办 三岁宝宝说话有点口吃怎么办 六岁说话重复第一个字怎么办 宝贝烧到39.5度怎么办 宝贝39度不退烧怎么办 两岁多小儿突然变得口吃怎么办 百度两周岁宝宝口吃怎么办 2岁宝宝偶尔结巴怎么办 两岁宝宝说话磕巴怎么办 宝宝两岁结巴了怎么办 人多说话就紧张怎么办 小孩拉尿不叫人怎么办 2岁宝宝说话有点结巴怎么办 两岁半的宝宝说话结巴怎么办 2个月宝宝怕洗澡怎么办 2岁宝宝不喜欢喝奶粉怎么办 宝宝断奶不喜欢喝奶粉怎么办 宝宝不喜欢奶粉的味道怎么办 四个月宝宝不喜欢吃奶粉怎么办 四岁宝宝有口臭怎么办 4个月宝宝口臭怎么办