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 the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). 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.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

题意:  求连续区间最大和,并输出这个区间左右端点的值

题解:  1、模拟,求出每一个正数的连续区间和,更新最大区间值并记录左右端点的值
  2、dp[i]表示区间以a[i]结尾的区间和,转移方程为dp[i]=max(dp[i],dp[i-1]+a[i]),并更新区间下标
注意:  1、当a[i]全为负值的时候,输出0 a[0] a[n-1]  2、  当输入为:  5  -1 -1 0 -1 -1  输出为:0 0 0 代码一(模拟)
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<map>
#define MAX 1000000
#define ll long long
using namespace std;
int a[10005];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    int ans=-1,l=0,r=n-1;
    int temp=0,first=0;
    for(int i=0;i<n;i++)
    {
        temp=temp+a[i];
        if(temp<0)
        {
            temp=0;
            first=i+1;
        }
        else if(temp>ans)
        {
            ans=temp;
            l=first;
            r=i;
        }
    }
    if(ans<0)
        ans=0;
    cout<<ans<<‘ ‘<<a[l]<<‘ ‘<<a[r]<<endl;
    return 0;
}
代码二(dp)
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<map>
#define MAX 1000000
#define ll long long
using namespace std;
int a[10005],l[10005],r[10005];
int dp[10005];//以a[i]结尾的最大连续区间和
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        dp[i]=a[i];//初始化
    }

    l[0]=r[0]=0;

    for(int i=1;i<n;i++)
    {
        if(dp[i-1]+a[i]>=dp[i])//如果a[i]>=0
        {
            dp[i]=dp[i-1]+a[i];
            l[i]=l[i-1];//区间左端点不变,右端点更新
            r[i]=i;
        }
        else//如果a[i]是负数,新建一个区间
        {
            l[i]=i;
            r[i]=i;
        }
    }
    int ans=-1,pos=0,flag=0;
    for(int i=0;i<n;i++)
    {
        if(dp[i]>ans)
        {
            flag=1;
            ans=dp[i];
            pos=i;
        }
    }
    //cout<<flag<<endl;
    if(flag==0)
        cout<<0<<‘ ‘<<a[0]<<‘ ‘<<a[n-1]<<endl;
    else
        cout<<ans<<‘ ‘<<a[l[pos]]<<‘ ‘<<a[r[pos]]<<endl;
    return 0;
}
 

原文地址:https://www.cnblogs.com/-citywall123/p/12103282.html

时间: 2024-10-26 10:39:22

1007 Maximum Subsequence Sum (25分) 求最大连续区间和的相关文章

[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

pta 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

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

PTA 1007 Maximum Subsequence Sum (25 分)

1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <vector> 5 #include <algorithm> 6 #include <cassert> 7 #include <queue> 8 using namespace std; 9 int n; 10 int main() 11 { 12 cin >>

1007 Maximum Subsequence Sum (25分)(动态规划DP)

#include <vector> #include<iostream> using namespace std; int main() { int k; cin>>k; int left_index=0,right_index=k-1,sum=-1,tmp=0,tmp_index=0; vector <int> num(k); for(int i=0;i<k;i++) { cin>>num[i]; tmp+=num[i]; if(tmp&

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)——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 <=

5-1 Maximum Subsequence Sum (25分)

Given a sequence of KK integers { N_1N?1??, N_2N?2??, ..., N_KN?K?? }. A continuous subsequence is defined to be { N_iN?i??, N_{i+1}N?i+1??, ..., N_jN?j?? } where 1 \le i \le j \le K1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which