九度online judge-最大连续子序列

题目来源:http://ac.jobdu.com/problem.php?pid=1011

我的代码:超时

//c语言中,数学函数除了求整数的绝对值函数abs()之外<abs() 定义在stdlib.h中>,其余的函数都在头文件 math.h 中定义,包括对浮点数求绝对值的函数fabs()。
//c++中,包含的相应的头文件为,原则是前面加c,同时去掉.h 。
//例如:
//#include <cstdlib>对应        #include <stdlib.h>
//#include <cmath>对应        #include <math.h>

#include <iostream>

#include <cstdlib>

using namespace std;

int m;

int a[10000];

int lesss,moree;

int maxx;

//int sum=0;

int pick(int low,int high,int& les,int& mor){//总是假设序列从一个正数开始

    if (low==high&&a[low]<0){

        m=0;

        les=0;

        mor=high;

        return 0;

    }

    if(low>high) return 0;

    int ma=a[low];

    int sum=0;

    int less=low;

    int more=low;


    while(a[++more]>=0&&more<=high){

    //    more=low+1;

        ma+=a[more];

    }

    more--;

    int i=more+1;

    while (a[more]==0&&more>low) more--;

    while (a[less]==0&&(less+1)<=more) less++;

    for(;i<=high;i++){

        if(a[i]>=0) break;

            sum+=a[i];

            

    }

    maxx=pick(i,high,lesss,moree);

    

    int summ=abs(sum);

    if(maxx>summ){

        if((ma-summ+maxx)>=maxx){

            ma=ma-summ+maxx;

        more=moree;

        }

        else if((ma-summ+maxx)<maxx){

            less=lesss;

            more=moree;

            ma=maxx;

        }

    }

    else if(maxx<=summ)

        if(ma<maxx){

            less=lesss;

            more=moree;

            ma=maxx;

        }

    les=less;

    mor=more;

    return ma;

}

int main(){

    int k;

    while(cin>>k){

        if (k==0) return 0;

        for(int i=0;i<k;i++)

            cin>>a[i];

        int less=0,more=k-1;

        int i;

        for(i=0;i<k;i++)

            if(a[i]>=0) break;

       m= pick(i,k-1,less,more);

       cout<<m<<" "<<a[less]<<" "<<a[more]<<endl;

    }

    return 0;

}


他人ac代码:

#include<stdio.h>
#define Max 10000
int main(){
         int maxsofar;
         int maxendinghere;
         int begin,end,temp;
         int x[Max];
         int i,n;
         int count;
 
         while ( scanf ( "%d" ,&n),n>0){
                 for (i=0;i<n;i++)
                         scanf ( "%d" ,&x[i]);
                 count = 0;
                 maxsofar = maxendinghere = 0;
                 begin = end = temp = 0;
                 for (i=0;i<n;i++){
                         if (x[i]<0) count++;
                         maxendinghere += x[i];
                         if (maxendinghere<=0){
                                 maxendinghere = 0;
                                 temp = i+1;
                         }
                         if (maxendinghere>maxsofar){
                                 maxsofar = maxendinghere;
                                 begin = temp;
                                 end = i;
                         }
                         if (maxendinghere==0&&maxsofar==0&&x[i]==0)
                                 end = begin = i;
                 }
                 if (count==n) printf ( "0 %d %d\n" ,x[0],x[n-1]);
                 else
                         printf ( "%d %d %d\n" ,maxsofar,x[begin],x[end]);
         }
}
本题比较简单的算法是用贪心算法。用递归当递归层数比较深时很容易超时。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值