数据结构练习 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 ...
随机推荐
- Android Butterknife框架配置
一.原理. 最近发现一个很好用的开源框架,蛮不错的,可以简化你的代码,是关于注解的.不多说直接进入使用步骤讲解. 二.步骤. 1.准备阶段,先到官网( http://jakewharton.githu ...
- 读书笔记-HBase in Action-第二部分Advanced concepts-(1)HBase table design
本章以山寨版Twitter为例介绍HBase Schema设计模式.广义的HBase Schema设计不仅仅包含创建表时指定项,还应该综合考虑Column families/Column qualif ...
- EBS OAF开发中实现參数式弹出窗体
EBS OAF开发中实现參数式弹出窗体 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 概览 參数式弹出窗体和嵌入式弹出窗体不一样,它拥有独立 ...
- Android中配置JDK和SDK的环境变量
JDK环境变量的配置: 右击"计算机"或"我的电脑",选择"属性"-->"高级"或"高级系统设置&quo ...
- 谈谈Angular关于$watch,$apply 以及 $digest的工作原理
这篇文章主要是面向那些刚开始学AngularJs和想要了解数据绑定(data-binding)是怎么工作的, 如果你已经熟悉如何使用angularjs了,我强烈建议你不用阅读了. angularjs使 ...
- WIN32 根据程序名(映像名称)终止外部程序
场景: 1.有时候需要调用外部程序,但是外部程序有可能崩溃挂起,这样这个进程就没法结束,所以再在下次调用前需要先结束之前调用的. 2.没考虑到权限问题,应该是只能终止当前用户启动的进程. #inclu ...
- iOS在xib或storyboard里为控件添加圆角、外框和外框颜色
如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以: layer.borderWidth 设置外框宽度属性 layer.cornerRadius 设置圆角属性 只要为属性 ...
- Java基础知识强化之网络编程笔记03:UDP之UDP协议发送数据 和 接收数据
1. UDP协议发送数据 和 接收数据 UDP协议发送数据: • 创建发送端的Socket对象 • 创建数据,并把数据打包 • 调用Socket对象的发送方法,发送数据包 • 释放资源 UDP协议接 ...
- 理解FTP协议
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5186117. ...
- JavaScript的DOM操作(一)
DOM:文档对象模型 --树模型文档:标签文档,对象:文档中每个元素对象,模型:抽象化的东西 一:window: 属性(值或者子对象):opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器 ...