zoukankan      html  css  js  c++  java
  • 51nod DP 最大子段和

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #define MAXN 50000
    using namespace std;
    int n;
    long long a[MAXN],sum[MAXN];
    /*
    d[j]表示以j为终点的sum中最大的.
    if(d[j]>0) d[j+1] = d[j]+a[j+1];
    else
        d[j+1] = a[j+1]
    */
    int main()
    {
        cin>>n;
        long long max = 0;
        //memset(a,0,sizeof(a));
        for(int j=0;j<n;j++)
        {
            cin>>a[j];
            if(j==0)
            {
                sum[0] = a[0];
                continue;
            }
            if(sum[j-1]>0)
                sum[j] = sum[j-1] + a[j];
            else
                sum[j] = a[j];
            if(sum[j]>max)
                max = sum[j];
        }
        cout<<max<<endl;
        return 0;
    }
  • 相关阅读:
    HDU 1198
    HDU 1863
    HDU 1879
    HDU 1233
    HDU 1232
    HDU 1829
    HDU 2473
    hdu 1829 A Bug's Life
    hdu 3038 How Many Answers Are Wrong
    hdu 1198 Farm Irrigation
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6128970.html
Copyright © 2011-2022 走看看