PAT 甲级 1007 Maximum Subsequence Sum
发布日期:2021-07-01 03:08:44 浏览次数:2 分类:技术文章

本文共 2821 字,大约阅读时间需要 9 分钟。

1007 Maximum Subsequence Sum (25 point(s))

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

Experiential Summing-up

This question involves dynamic programming and that is a classical problem of solving the maximum subsequence sum. We must pay attention to two requirements brought by rubric. Firstly, if maximum is a negative number, output 0 and the first and the last numbers of the whole sequence. This one is OK. The second request can be solved by traversal from subscript 0 to subscript k, Jump the loop as soon as find the element whose sum is equal to maxsum. That's all~

 (The purpose of using English to portray my solution is that to exercise the ability of my expression of English and accommodate PAT advanced level's style.We can make progress together by reading and comprehending it. Please forgive my basic grammar's and word's error. Of course, I would appreciated it if you can point out my grammar's and word's error in comment section.( •̀∀•́ ) Furthermore, Big Lao please don't laugh at me because I just a English beginner settle for CET6    _(:з」∠)_  )

Accepted Code

#include 
#include
const int maxn=10010;struct node{ int start,end; int sum; int value;}dp[maxn];int main(){ int k; scanf("%d",&k); for(int i=0;i
=dp[i].value) { dp[i].sum=dp[i].value+dp[i-1].sum; dp[i].start=dp[i-1].start; dp[i].end=i; } else { dp[i].sum=dp[i].value; dp[i].start=i; dp[i].end=i; } maxsum=maxsum>dp[i].sum?maxsum:dp[i].sum; } if(maxsum<0) { printf("0 %d %d\n",dp[0].value,dp[k-1].value); } else { printf("%d ",maxsum); for(int i=0;i

 

转载地址:https://meteorrain.blog.csdn.net/article/details/86521169 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PAT 甲级 1008 Elevator
下一篇:PAT 甲级 1006 Sign In and Sign Out

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月18日 15时50分56秒