PAT甲题题解-1007. Maximum Subsequence Sum (25)-求最大子区间和

题意:给出n个数,求最大连续的子区间和,并且输出该区间的第一个和最后一个数。

如果所有数都小于0,那么则输出0,第一个数和最后一个数。

看数据k的范围,就知道肯定不能两层for循环来求区间和,O(n^2)的复杂度肯定超时
所以这里肯定要求一遍for循环就能知道结果
定义区间l和r,sum为目前[l,r]之间的和
一开始l=r=0,sum=a[0]
接下来,对于当前第i个数字a[i]
如果sum+a[i]>a[i],那么就将a[i]一起加入到区间中去。
如果sum+a[i]<a[i],那么还不如不加,直接重新从a[i]开始,sum=a[i]。
更新对应的区间,顺便比较一下当前的sum和maxsum即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=10000+5;
int a[maxn];
int maxsum=-INF;
int maxl,maxr;
int l,r;
int main()
{
    int n,val;
    int sum;
    scanf("%d",&n);
    scanf("%d",&a[0]);
    sum=a[0];
    l=0;
    r=0;
    maxl=l;
    maxr=r;
    maxsum=sum;
    bool allnegative=true;
    if(a[0]>=0)
        allnegative=false;
    for(int i=1;i<n;i++){
        scanf("%d",&a[i]);
        if(a[i]>=0)
            allnegative=false;
//printf("l:%d r:%d sum:%d %d:val\n",l,r,sum,a[i]);
        if(sum+a[i]>a[i]){
            sum+=a[i];
            r++;
            if(sum>maxsum){
                maxl=l;
                maxr=r;
                maxsum=sum;
            }
        }
        else{
            sum=a[i];
            l=r=i;
            if(sum>maxsum){
                maxl=l;
                maxr=r;
                maxsum=sum;
            }
        }
    }
    //如果都小于0,按照题目要求,输出0,第一个和最后一个数
    if(allnegative)
        printf("0 %d %d\n",a[0],a[n-1]);
    else
        printf("%d %d %d",maxsum,a[maxl],a[maxr]);
    return 0;
}

时间: 2024-10-12 08:56:45

PAT甲题题解-1007. Maximum Subsequence Sum (25)-求最大子区间和的相关文章

PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数

题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里用的是dfs,dfs的复杂度只要O((m+n)*k)这里k是指因为有k个点要查询,每个都要求一下删除后的联通分支数.题目没给定m的范围,所以如果m很大的话,dfs时间会比较小. for一遍1~n个点,每次从一个未标记的点u开始dfs,标记该dfs中访问过的点.u未标记过,说明之前dfs的时候没访问过

1007. Maximum Subsequence Sum (25)——PAT (Advanced Level) Practise

题目信息: 1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <=

[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1007 Maximum Subsequence Sum (25 分) Description Given a sequence of $K$ integers { $N_{1}?$, $N_{2}?$, $...$, $N_{K}$ }. A continuous subsequ

1007 Maximum Subsequence Sum (25分) 求最大连续区间和

1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has th

1007 Maximum Subsequence Sum (25)(25 分)

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence

1007. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For examp

1007. Maximum Subsequence Sum (25)(DP)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For examp

数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl

1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl