数据结构练习 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 -21Sample Output:
10 1 4
#include<iostream>
using namespace std;
int MaxsubseqSum2(int a[],int k,int *first,int *last){
int ThisSum=,MaxSum=;
int c=,d=,e=;
for(int i=;i<k;i++){
ThisSum+=a[i];
if(ThisSum>MaxSum){
MaxSum=ThisSum;
*last=a[i];
d=i;
}else if(ThisSum<){
ThisSum=;
e++;
}else if(ThisSum==MaxSum &&MaxSum==){
MaxSum=ThisSum;
*last=a[i];
d=i;
} }
if(e==){
*first=a[];
}
ThisSum=;
for(int i=;i<d;i++){
ThisSum+=a[i];
if(ThisSum<){
ThisSum=;
*first=a[i+];
} }
if(MaxSum== && a[d]<){
*first=a[];
*last=a[k-];
}
return MaxSum;
}
int main(){
int k;
int *a;
int num;
int FirstNum=,LastNum=;
cin>>k;
a = new int[k];
for(int i=;i<k;i++){
cin>>a[i];
}
num=MaxsubseqSum2(a,k,&FirstNum,&LastNum);
cout<<num<<" "<<FirstNum<<" "<<LastNum;
delete [] a;
return ;
}
卡在了测试点3,4,5上面
测试点3,是考虑数字相同的时候。最开始的姥姥讲的那个算法中,当数字相同的时候,我输出的最小子列和的第一个数会是0。(这也是一种特殊的情况)
测试点4,是当数字全为负数的时候,first和last为数列的第一个数和最后的一个数字
测试点5,当0,负数全都在的时候,这是时候first和last应该都为0,而不是像4中一样,first和last为数列的第一个数和最后的一个数字。
数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum (25分) Given ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 01-复杂度2 Maximum Subsequence Sum
01-复杂度2 Maximum Subsequence Sum (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...
- PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
随机推荐
- SystemTimeToFileTime、FileTimeToLocalFileTime、LocalFileTimeToFileTime三函数的跨平台实现
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #include & ...
- 如何学会web前端开发
如何学会web前端开发 http://jingyan.baidu.com/article/b7001fe17623970e7282dd0c.html http://www.yangqq.com/dow ...
- android 嵌套 apk 从一个apk启动另外一个apk
a.apk-主应用 b.apk-被启动应用 主要思想:把b.apk放到assets目录下,由于有大小限制(1M),所以改名成b.mp3(因为mp3,jpg,png,mp4等不会检查,不会限制大小), ...
- web开发性能优化---扩展性能篇
1.实现代码分离 一个成熟的软件开发团队一般都不会全然手写代码.这里讲的代码分离仅仅要是开发中用到的小技巧,通过底层框架+手工代码方式结合实现高速开发和高速扩展. Code目录内文件不同意改动,目录主 ...
- MAC上更新Ruby失败解决办法
尝试用RVM升级Ruby http://blog.csdn.net/lissdy/article/details/9191351 如果碰到下面问题 Searching for binary rubie ...
- vc++ 最小化到托盘的详细实现
在VC++中,想实现将MFC最小化到系统托盘,需要调用NOTIFYICONDATA类,并注册相应的消息,以下详细讲解如何实现: 1.声明一个NOTIFYICONDATA类,也就是NOTIFYICOND ...
- Linux重复执行上一条命令
执行刚刚执行的一条命令: !! 执行最近一个以指定字符串开头的命令(比如man) !man !m 引用上一个命令的最后一个参数 !$ <ESC>, .
- sql if
SELECT a.id, a.EduSiteNo, a.EduSiteName, a.SchoolId, a.LinkMan, a.Tel, a.Mobile, a.Fax, a.Address, C ...
- HTML+CSS基础学习笔记(2)
一.无序列表标签ul <ul> <li>信息</li> <li>信息</li> ...... </ul> 代码解释:每项< ...
- 20、CSS
CSS 层叠样式表(Cascading Style Sheets). 用于定义显示HTML样式. DIV和SPAN div是块级元素. span是行级元素. 将一些页面中的内容包裹起来统一设置样式. ...