zoukankan      html  css  js  c++  java
  • poj 2479 Maximum sum

    题目链接:http://poj.org/problem?id=2479

            基础DP题,跟以前做的最大子段和差不多。数据量比较大,一开始用cin,cout输入输出,超时,改成scanf就好了。

    #include<iostream>
    #include<fstream>
    #include<algorithm>
    #include<cstring>

    using namespace std ;
    int dp[50005], a[50005] ;
    int main(){
        //fstream cin("x.in") ;
        int t, n, i ;
        memset(dp, 0sizeof(dp)) ;
        memset(a, 0sizeof(a)) ;
        scanf("%d", &t) ;
        while(t--){
            scanf("%d", &n) ;
            int ans = -9999999 ;
            int tmax = -9999999 ;
            int sum = 0 ;
            for(i=1; i<=n; i++){     //从左到右最大子段和
                scanf("%d", &a[i]) ;
                dp[i] = max(dp[i-1]+a[i], a[i]) ;
            }
            for(i=n; i>1; i--){      //从右到左
                sum = sum<0?a[i]:sum+a[i] ;
                tmax = max(sum, tmax) ;
                ans = max(dp[i-1]+tmax, ans) ;//选取两段和的最大值
            }
            cout << ans << endl ;
        }
    }
  • 相关阅读:
    CF359B Permutation
    CF859C Pie Rules
    Contest 156
    Contest 155
    Range Module
    python-环境
    Git 使用
    Contest 154
    生命是一种长期而持续的累积过程
    Contest 153
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2190542.html
Copyright © 2011-2022 走看看