【PAT】1007. Maximum Subsequence Sum (25)
2018-03-22 13:23
435 查看
题目描述
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 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.
翻译:给一组K个数字的数列{ N1, N2, …, NK }。一个连续数列被定义为 { Ni, Ni+1, …, Nj } ,1 <= i <= j <= K。最大连续数列是连续数列中元素之和最大的数列。例如,给定一个数列 { -2, 11, -4, 13, -5, -2 },它的最大连续数列为{ 11, -4, 13 } ,及最大和为20。
INPUT FORMAT
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.翻译:每个输入文件包括一组测试数据。每组测试数据包括两行。第一行包含一个正整数K (<= 10000),第二行包含K个数字,由一个空格隔开。
OUTPUT FORMAT
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.翻译:对于每组输入数据,在一行内输出最大值,包括最大连续数列的第一个数和最后一个数。数字必须用空格隔开,末尾不能有多余空格。万一最大连续数列不是唯一的,输出最早出现的i和j(就像样本数据那样)。如果所有K个数均为负数,则它的最大和被定义为0,并且你需要输出整个数列的第一个数和最后一个数。
Sample Input:
10-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4解题思路
这道题是一道数学题,根据当前输入值a和当前连续数列和的大小temp来判断。如果a<0,则判断max(Max,temp),如果temp+a>0,则保存temp+a,否则让temp=0;如果a>0,则判断temp是否大于0,如果大于0,temp=temp+a,否则temp=a。剩下的代码就是用来保存起始点和终点的了。#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #define INF 99999999 using namespace std; int N,pre=0,pro=0,temp=0,first,last,temppre=0,temppro=0,Max=0,flag=0; int main(){ scanf("%d",&N); int a; for(int i=0;i<N;i++){ scanf("%d",&a); if(i==0)first=a; if(i==N-1)last=a; if(a<0){ if(Max<temp){ pre=temppre; pro=temppro; Max=temp; } if(temp+a>0){ temp=temp+a; temppro=a; } else { temp=0; temppre=temppro=a; } } else{ if(!flag)flag=1; if(temp>0){ temp=temp+a; temppro=a; } else { temp=a; temppre=temppro=a; } if(Max<temp){ pre=temppre; pro=temppro; Max=temp; } } } if(Max==0&&!flag)printf("%d %d %d\n",Max,first,last); else if(Max==0)printf("0 0 0\n"); else printf("%d %d %d\n",Max,pre,pro); return 0; }
相关文章推荐
- PAT (Advanced Level) Practise 1007. Maximum Subsequence Sum (25)
- PAT-A 1007. Maximum Subsequence Sum (25)
- PAT-1007. Maximum Subsequence Sum (25)
- 【PAT Advanced Level】1007. Maximum Subsequence Sum (25)
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 解题报告
- PAT-1007. Maximum Subsequence Sum (25)
- PAT(甲级)1007. Maximum Subsequence Sum (25)
- [PAT]1007. Maximum Subsequence Sum (25)@Java
- PAT - 甲级 - 1007. Maximum Subsequence Sum (25)
- PAT 1007. Maximum Subsequence Sum (25)(dp动态规划)
- PAT_A 1007. Maximum Subsequence Sum (25)
- PAT甲题题解-1007. Maximum Subsequence Sum (25)-求最大子区间和
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)
- PAT-Java-1007. Maximum Subsequence Sum (25)
- 【PAT】【Advanced Level】1007. Maximum Subsequence Sum (25)
- PAT 1007. Maximum Subsequence Sum (25)
- PAT甲级-1007. Maximum Subsequence Sum (25)最大子序列和
- PAT 1007. Maximum Subsequence Sum (25)
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
- pat 1007. Maximum Subsequence Sum (25)