hdu 1024 循环数组+简单dp

注意常熟复杂度,因为数据量大,常数复杂度过大也会超时...

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define MAX 1000007

using namespace std;

int m,n,maxn;
int now[MAX];
int a[MAX];
int pre[MAX];

int main ( )
{
    while ( ~scanf ( "%d%d" , &m , &n ) )
    {
        for ( int i = 1 ; i <= n ; i++ ) 
            scanf ( "%d" , &a[i] );
        memset ( now , 0 , sizeof ( now ) );
        memset ( pre , 0 , sizeof ( pre ) );
        for ( int i = 1 ; i <= m ; i++ )
        {
            maxn = -99999999;
            for ( int j = i ; j <= n ; j++ )
            {
                now[j] = max ( now[j-1]+a[j] , pre[j-1]+a[j] );
                pre[j-1] = maxn;
                maxn = max ( now[j] , maxn );
            }
        }
        printf ( "%d\n" , maxn );
    }
}


你可能感兴趣的:(C++,动态规划,循环数组)