数据结构练习 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 ...
随机推荐
- flash builder4.7安装git插件
如果直接点击Help > Eclipse Marketplace,然后搜索Egit, 以这种方式安装是会失败的!!! 因为版本兼容的问题.依次点击Help > About Flash Bu ...
- 创业公司求IOS、Android攻城狮一枚
地点:北京CBD附近(地铁1号线大望路) 魅力值:送珍贵期权 你要会: 1. IOS或者Android开发 2.可以处理类似微视/秒拍的视频录制功能 3.熟悉jso ...
- im消息丢失插件
https://github.com/laughin/mocamsg mocamsg Moca message interceptor Openfire网络不好的情况下经常丢消息,一般情况都是服务器端 ...
- 获取DLL的文件路径以及调用DLL的文件路径
如何在DLL中,获取DLL本身的路径以及调用DLL的文件的路径呢?主要通过GetModuleFileName(HMODULEhModule,LPTSTR lpFilename,DWORD nSize) ...
- sql like '%x%'优化
好久没写点什么了.唉(此处省略无数,一切尽在苦逼中...) 说说sql中的全匹配优化吧.在sql server进行模糊查询的时候,如果是进行全匹配的话,那么肯定会用到like.我们知道like '%张 ...
- [转]JavaScript 的同源策略
同源策略限制了一个源(origin)中加载文本或脚本与来自其它源(origin)中资源的交互方式. 同源定义 如果两个页面拥有相同的协议(protocol),端口(如果指定),和主机,那么这两个页面就 ...
- struts 2读书笔记-----struts2的开发流程
一.将struts 2的lib文件夹下地commons-fileupload.jar.commons-io.jar.freemarker.jar.javassist.jar.ognl.jar.stru ...
- SqlSugar常用增删改操作
一.添加数据 特别说明: 1.特别说明:对于自增长列的表插入数据后,当前自增长列的字段,仍旧为0,但可以通过Insert方法的返回值来获取 SqlSugarClient db = SugarConte ...
- 编写适合windows 7 平台的软件,给程序添加UAC认证
Delphi程序必须在资源里面嵌入MANIFEST信息 一. 首先编辑一个文件,内容如下: <?xml version="1.0" encoding="UTF-8& ...
- Object-C内存管理基础
如果你通过手工alloc的方式分配内存实例化创建一个对象,之后你需要release这个对象,同理你也不能手工释放(release)一个能自动释放(autoreleased)的对象,因为这个样子会使你的 ...