pat 1007. Maximum Subsequence Sum
来源:互联网 发布:计算机二级java模拟题 编辑:程序博客网 时间:2024/05/29 13:31
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.
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 -21Sample Output:
10 1 4
#include <stdio.h>#include <stdlib.h>int main(){ //freopen("F:/CppProject/data/1007.txt","r",stdin); int k,data[10002]; scanf("%d",&k); for(int i=0;i<k;i++) { scanf("%d",&data[i]); } int start=0,end=k-1,s=0,e=k-1,max=0,tmp=0; int p=0; while(p<k) { if(tmp>=0) { tmp+=data[p]; e=p; } else { tmp=data[p]; s=p; e=p; } if(max<tmp) { max=tmp; start=s; end=e; } p++; } if(max==0) { for(int i=0;i<k;i++) { if(data[i]==0) { start=i; end=i; break; } } } printf("%d %d %d\n",max,data[start],data[end]); return 0;}
- (PAT)1007. Maximum Subsequence Sum
- (PAT)1007. Maximum Subsequence Sum
- pat 1007. Maximum Subsequence Sum
- PAT 1007. Maximum Subsequence Sum
- PAT 1007. Maximum Subsequence Sum
- PAT 1007. Maximum Subsequence Sum
- PAT 1007. Maximum Subsequence Sum
- 【PAT】1007. Maximum Subsequence Sum
- PAT - Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum (25)-PAT
- 【PAT】1007. Maximum Subsequence Sum (25)
- PAT 1007. Maximum Subsequence Sum (25)
- pat 1007. Maximum Subsequence Sum (25)
- PAT A 1007. Maximum Subsequence Sum (25)
- PAT(Advanced level) 1007. Maximum Subsequence Sum*
- PAT 1007. Maximum Subsequence Sum (25)
- PAT 1007. Maximum Subsequence Sum (25)
- PAT 1007. Maximum Subsequence Sum (25)
- 解决百度排名停滞不前的三方法
- android4.0 屏幕切换(避免重走Activity的生命周期)
- Hadoop计算中的Shuffle过程
- SIP常用消息实例参考 5、ACK消息
- jsoup选择器来抓取网页中的数据
- pat 1007. Maximum Subsequence Sum
- vector底层实现
- MFC中实现字符串查找
- 分享java压力测试模版类 版本2
- 教你如何做有效内链
- StringUtils的isBlank与isEmply
- SIP常用消息实例参考 6、BYE消息
- android httpClient 支持HTTPS的2种处理方式
- Mac OS X 10.10优胜美地如何完美接管iphone上的电话和短信