最大序列和[1077@Jobdo]

//@Jobdo 清华06 典型的DY问题
//本题的时间复杂度为O(n).空间复杂度为O(1).
#include<stdio.h>
#include<algorithm>
using namespace std;

typedef long long int T;
T s[2];

int main()
{
    int n;
    while(~scanf("%d",&n)){
        bool first=true;
        T    myMax, tp;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&tp);
            if(first){
                myMax=s[1]=tp;
                first=false;
            }
            else
            {
                s[1]= s[0]>0 ? s[0]+tp : tp;
                if(myMax<max(s[1],s[0]))
                    myMax=max(s[1],s[0]);
            }
            s[0]=s[1];
        }
        printf("%lld\n",myMax);
    }//end while

    return 0;
}

posted on 2012-03-07 16:34 DjvuLee 阅读(155) 评论(0)  编辑 收藏 引用 所属分类: 动态规划


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿

随笔分类(13)

随笔档案(19)

文章分类(2)

文章档案(1)

搜索

最新评论

评论排行榜