http://pat.zju.edu.cn/contests/pat-practise/1007
来源:互联网 发布:java.io jar包 编辑:程序博客网 时间:2024/05/01 04:32
1007. Maximum Subsequence Sum (25)
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<iostream>
- #include<cstdio>
- #include<memory.h>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #include<cmath>
- #include<cstdlib>
- #include<string>
- using namespace std;
- #define MAX 0x6FFFFFFF
- int a[10001];
- int main(){
- //freopen("in.txt", "r", stdin);
- int k;
- while(cin>>k){
- bool flag = true;
- for(int i=0;i<k;++i){
- scanf("%d", a+i);
- if(a[i]>=0)
- flag = false;
- }
- if(flag){
- printf("0 %d %d\n", a[0], a[k-1]);
- continue;
- }
- int sum = 0;
- int max = -MAX;
- int start, end;
- int s = 0;
- for(int i=0;i<k;++i){
- sum += a[i];
- if(sum>max){
- max = sum;
- start = s;
- end = i;
- }
- if(sum<0){
- sum = 0;
- s = i+1;
- }
- }
- printf("%d %d %d\n", max, a[start], a[end]);
- }
- //fclose(stdin);
- return 0;
- }
- http://pat.zju.edu.cn/contests/pat-practise/1007
- http://pat.zju.edu.cn/contests/pat-practise/1030
- http://pat.zju.edu.cn/contests/pat-practise/1034
- http://pat.zju.edu.cn/contests/pat-practise/1016
- http://pat.zju.edu.cn/contests/pat-practise/1032
- http://pat.zju.edu.cn/contests/pat-practise/1037
- http://pat.zju.edu.cn/contests/pat-practise/1021
- http://pat.zju.edu.cn/contests/pat-practise/1040
- http://pat.zju.edu.cn/contests/pat-practise/1038
- http://pat.zju.edu.cn/contests/pat-practise/1025
- http://pat.zju.edu.cn/contests/pat-practise/1023
- http://pat.zju.edu.cn/contests/pat-practise/1029
- http://pat.zju.edu.cn/contests/pat-practise/1035
- http://pat.zju.edu.cn/contests/pat-practise/1024
- http://pat.zju.edu.cn/contests/pat-practise/1031
- http://pat.zju.edu.cn/contests/pat-practise/1028
- http://pat.zju.edu.cn/contests/pat-practise/1020
- http://pat.zju.edu.cn/contests/pat-practise/1015
- 夏五月,崔杼弑君
- http://pat.zju.edu.cn/contests/pat-practise/1010
- 美国副国务卿本月16日至17日将访华-亚太局势-中日关系-钓鱼岛问题
- 【Java】程序员应该掌握的10项技能
- http://pat.zju.edu.cn/contests/pat-practise/1009
- http://pat.zju.edu.cn/contests/pat-practise/1007
- http://pat.zju.edu.cn/contests/pat-practise/1005
- 介绍Servlet容器
- Emacs org进阶
- http://pat.zju.edu.cn/contests/pat-practise/1004
- 数据结构——KMP算法的实现
- 28个Unix/Linux的命令行神器
- http://pat.zju.edu.cn/contests/pat-practise/1003
- pragma comment的使用