码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ1024(最大M子段和)

时间:2015-07-31 20:10:52      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

模板:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX(a,b) (a>b)?a:b
const int SIZE=1000000+16;
const int INF=0x30303030;
int a[SIZE];
int dp[SIZE];
int rec[SIZE];
int main()
{
    int n,m;

    while(scanf("%d %d",&m,&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            rec[i]=dp[i]=0;        
        }
        dp[0]=0;
        rec[0]=0;
        int ans;
        for(int i=1;i<=m;i++)
        {
            ans=-INF;
            for(int j=i;j<=n;j++)
            {    
                
                dp[j]=MAX(dp[j-1]+a[j],rec[j-1]+a[j]);
                rec[j-1]=ans;
                ans=MAX(ans,dp[j]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

HDOJ1024(最大M子段和)

标签:

原文地址:http://www.cnblogs.com/program-ccc/p/4692957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!