Maximum Subsequence Sum
来源:互联网 发布:蓝凌软件下载 编辑:程序博客网 时间:2024/06/07 18:55
题目:
01-复杂度2 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 -21
Sample Output:
10 1
误解 :0 0 5 7 开始以为不算上 0 0
解题:
定义最基本变量和准确含义:MaxSum,maxl,maxr sum,cl,cr
方法:在线处理
#include <stdlib.h>#include<stdio.h>int* MaxSeqSum(int list[], int n){ int MaxSum, sum; int maxl,maxr; int cl,cr ; int *result=new int[3]; int i; MaxSum=list[0]-1;//MaxSum=-1000000 maxl =-1;//maxl=0 maxr = -1;//maxr=0 sum=0; cl = 0; //cl 和cr记录当前可保留、可继续探索的序列位置 cr = 0; for( i=0;i<=n-1;i++){ if(sum+list[i]>=0){ cr=i;sum=sum+list[i]; if(sum>MaxSum ){ MaxSum=sum; maxl=cl; maxr=cr; } } else { sum=0; cl =i+1; } } if(MaxSum<0){ result[1]=list[0]; result[2]=list[cl-1]; result[0]=0; } else{ result[0]=MaxSum; result[1]=list[maxl]; result[2]=list[maxr]; } return result;}int main(){ int n=0,a[100000]; scanf("%d",&n); int i; for (i=0;i<n;i++ ) scanf("%d",&a[i]); int *result=MaxSeqSum(a,n); int k; for(k=0;k<2;k++){ printf("%d ",result[k]); } printf("%d",result[2]); delete[] result; // system("pause"); return 0;}
- Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- PAT1007. Maximum Subsequence Sum
- PAT_1007: Maximum Subsequence Sum
- 1007 Maximum Subsequence Sum
- PAT - Maximum Subsequence Sum
- PAT1007. Maximum Subsequence Sum
- Maximum Subsequence Sum (25)
- Maximum Subsequence Sum
- 1007.Maximum Subsequence Sum
- Maximum Subsequence Sum
- pat1007 Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- Maximum Subsequence Sum
- PAT——1041. 考试座位号(15)
- android bug:java.lang.IllegalArgumentException: Unknown service start result: 4
- C语言中的求图像周长问题(ZOJ 1047)
- 论「版本号」的正确使用方式
- 从零开始前端学习[42]:关于浮动元素在窗口缩小情况下,布局下沉的深思
- Maximum Subsequence Sum
- 多线程练习题目
- M
- jfinal VS spring mvc
- 网络编程_进程基础
- 02、人人都会设计模式:单例模式--SingleTon
- Rem布局原理
- http get和post的区别
- 深入理解JAVA虚拟机--运行时数据区域