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

    给出n个数,求其m个子段和的最大值。

    动态规划问题

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define INF 0x7fffffff
    #define N 1000000+5
    using namespace std;
    int now[N];//包含a[j]的最大值
    int lstmax[N];//前j个的可以不包含a[j]的最大值
    int a[N];//储存输入的数字
    int mmax;//每一轮的最大值
    int main() {
        int m, n;
        freopen("C:\Users\super\Documents\CB_codes\in.txt", "r", stdin);
        while( ~ scanf("%d%d", &m, &n)) {
            for(int i = 1; i <= n; i ++) {
                scanf("%d", &a[i]);
            }
            now[0] = 0;
            memset(lstmax, 0, sizeof(lstmax) );
            for(int i = 1; i <= m; i ++) {
                mmax = -INF;
                for(int j = i; j <= n; j ++) {
                    now[j] = max(now[j-1] + a[j], lstmax[j-1] + a[j] );//包含a[j]的最大值
                    lstmax[j-1] = mmax;//上一轮的最大值,j-1
                    mmax = max(mmax, now[j]);//这一轮的最大值,j
                }
            }
            printf("%d
    ", mmax);
        }
    
    
        fclose(stdin);
        return 0;
    }
    ---------------- 人们生成的最美好的岁月其实就是最痛苦的时候,只是事后回忆起来的时候才那么幸福。
  • 相关阅读:
    mojoportal中弹出窗口
    css 层居中
    mojoportal中添加自定义javascript
    C#执行cmd [转载]
    异步委托 学习笔记
    Windows Sysinternals
    有关int,Int32的疑惑解答
    WEB Debug tools汇总
    规范很重要
    [笔记]VGA 接口电阻网络阻抗
  • 原文地址:https://www.cnblogs.com/livelihao/p/5302348.html
Copyright © 2011-2022 走看看