zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

    简单DP。

    注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    using namespace std;
    
    const int maxn=10000;
    int n,a[maxn],sum,ans1,ans2;
    int dp[maxn];
    
    int main()
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        dp[1]=a[1];
        for(int i=2; i<=n; i++) dp[i]=max(dp[i-1]+a[i],a[i]);
        int Max=-999999999;
        for(int i=1; i<=n; i++) Max=max(Max,dp[i]);
        if(Max<0) printf("0 %d %d
    ",a[1],a[n]);
        else
        {
            for(int i=1; i<=n; i++)
            {
                if(dp[i]==Max)
                {
                    ans2=a[i]; int sum=0;
                    for(int j=i; j>=0; j--)
                    {
                        sum=sum+a[j];
                        if(sum==Max) { ans1=a[j]; break; }
                    }
                    break;
                }
            }
            printf("%d %d %d
    ",Max,ans1,ans2);
        }
        return 0;
    }
  • 相关阅读:
    NSURLSession实现文件上传
    JS中如何判断null、undefined与NaN
    jquery
    url操作等
    设计模式
    javaScript类型转换
    jQuery.noop
    JavaScript严谨模式(Strict Mode)提升开发效率和质量
    Data URI
    e.target e.currenttarget
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5496039.html
Copyright © 2011-2022 走看看