【一只蒟蒻的刷题历程】 【PAT (Advanced Level) Practice】 1007 最大子序列总和

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


题目大意:

1007最大子序列总和(25分)

给定一个由K个整数组成的序列{N-1,N 2,…,NK}。连续子序列定义为{N i,N i + 1,…,N j}}其中1≤i≤j≤K。最大子序列是具有最大元素和的连续子序列。例如,给定序列{-2,11,-4,13,-5,-2},其最大子序列为{11,-4,13},最大和为20。

现在,您应该找到最大的和,以及最大子序列的第一个和最后一个数字。

输入规格:

每个输入文件包含一个测试用例。每个案例占用两行。第一行包含一个正整数K(≤10000)。第二行包含K个数字,以空格分隔。

输出规格:

对于每个测试用例,在一行中输出最大和,以及最大子序列的第一个和最后一个数字。数字必须用一个空格分隔,但行尾不能有多余的空格。如果最大子序列不是唯一的,则输出索引i和j最小的子序列(如示例情况所示)。如果所有K个数字均为负,则其最大和定义为0,并且应该输出整个序列的第一个和最后一个数字。

样本输入:

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

样本输出


10 1 4


思路:

坑点4 和 5:
一个是所有数为负数 ,输出 0 第一个数 最后一个数;
例子:-2 -3 -5 输出 0 -2 -5;
一个是至少有一个是0,其他都是负数或者0,输出0 0 0;
例子:-2 0 -3 输出 0 0 0;


代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int maxn=10100;
int main() 
{
   int n,MAX=0,flag=0,flag1=0;  //两个flag解决 坑点4和5
   int a[maxn],dp[maxn];   
   cin>>n; 
   for(int i=1;i<=n;i++)
   {
   	   cin>>a[i];
   	   if(a[i]>0) flag=1;   //flag=0 表示全部数都非正(0或负)
   	   if(a[i]==0) flag1=1; //记录是否有0
   }
   
   if(flag1==1 && flag==0)  //有0 非正(0或负)
   {
   	  cout<<0<<" "<<0<<" "<<0;  //输出0 0 0
	  return 0;
   }
   else if(flag==0 && flag==0) //无0 全负
   {
   	  cout<<0<<" "<<a[1]<<" "<<a[n];  //输出0 第一个数 最后一个数
	  return 0;
   }
    dp[0]=0; //前0个数最大为0
    
    int position;  //记录最大子序列的最后一个数
    for(int i=1;i<=n;i++)
    {
    	dp[i] = max(a[i] , dp[i-1]+a[i]); 
    	//前i个最大子序列为 max(第i个数,第i个数+前i-1个数的最大值)
    	if(MAX < dp[i]) //更新最大值
    	{
    		MAX=dp[i]; 
    		position=i; //更新位置
		}
	}
	//下面是为了从最后一个数往前累加,直到等于MAX,就找到了第一个数
    int sum=0;   //总和
    int wei=position; //先把最后一个数记录下来
	while(position)
	{
		sum+=a[position]; //累加
		if(sum==MAX) //和等于MAX,结束
		break;
		position--; //一定要放下面,如果放break前面就可能会多减1
	}  
    cout<<MAX<<" "<<a[position]<<" "<<a[wei]; //输出
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章