zoukankan      html  css  js  c++  java
  • Max Sum Plus Plus

    DP题自己实在想不出来,看大神代码才能勉强理解一下

       #include <iostream>  
        #include <cstdio>  
        #include <cstring>  
          
        using namespace std;  
          
        #define N 1000000  
        #define INF 0x7fffffff  
          
        int a[N+10];  
        int dp[N+10],Max[N+10];//max( dp[i-1][k] ) 就是上一组 0....j-1 的最大值。  
          
        int main()  
        {  
            int n,m,mmax;  
            while (~scanf("%d%d",&m,&n))  
            {  
                for (int i=1;i<=n;i++)  
                {  
                    scanf("%d",&a[i]);  
                }  
                memset(dp,0,sizeof(dp));  
                memset(Max,0,sizeof(Max));  
                for (int i=1;i<=m;i++)//分成几组  
                {  
                    mmax=-INF;  
                    for (int j=i;j<=n;j++)//j个数分成i组,至少要有i个数  
                    {  
                        dp[j]=max(dp[j-1]+a[j],Max[j-1]+a[j]);  
                        Max[j-1]=mmax;  
                        mmax=max(mmax,dp[j]);  
                    }  
                }  
                printf ("%d
    ",mmax);  
            }  
            return 0;  
        }  
  • 相关阅读:
    hihocoder 1038
    hihocoder 1039
    poj 2774
    bzoj 4690&&4602
    poj 2417
    STL
    poj 1026
    poj 1064
    poj 1861(prim)
    poj 1129
  • 原文地址:https://www.cnblogs.com/nr1999/p/9034585.html
Copyright © 2011-2022 走看看