数据结构练习 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 ...
随机推荐
- HTML5 API 浏览器支持情况检测
HTML5发展到现在,虽说没有大规模的普及,但在我们日常生活中,也很容易见到了,HTML5的游戏.网站.应用也是层出不穷.而作为前端人员,也应该多了解这些API为以后应用打基础,下面我将给大家介绍 H ...
- POJ 3734
题目的大意: 给定待粉刷的n个墙砖(排成一行),每一个墙砖能够粉刷的颜色种类为:红.蓝.绿.黄, 问粉刷完成后,红色墙砖和蓝色墙砖都是偶数的粉刷方式有多少种(结果对10007取余). 解题思路: 思路 ...
- Pilin —— 一个基于Xmpp openfire smack的即时聊天工具
https://github.com/whfcomm/Pilin
- [ES7] Exploring ES2016 Decorators
Original artial --> link How descorator looks like: @mydecorator function myFun(){ ... } Descorat ...
- getline和get的区别
#include<iostream> #include<fstream> #include<cstring> using namespace std; int ma ...
- mysql wait_timeout和interactive_timeout总结
(1)interactive_timeout:参数含义:服务器关闭交互式连接前等待活动的秒数.交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE ...
- ctkPlugin插件系统实现项目插件式开发
插件式开发体会: 自开始写[大话QT]系列就开始接触渲染客户端的开发,说是开发不如更多的说是维护以及重构,在接手这块的东西之前自己还有点犹豫,因为之前我一直认为客户端嘛,没什么技术含量,总是想做比较有 ...
- oracle 11g 报错记录
1.ORA-01034: ORACLE not available sqlplus "sys/password as sysdba" 2.ORA-00119: invalid sp ...
- DedeCMS文章标题前增加所属栏目名称链接
问题描述:在调用文章标题的时候,我想实现这样的形式:“[国内新闻]站长无忧真的是一个不错的站”,其中“国内新闻”是标题的所属栏目,并且加链接: 解决方法: [field:typelink /]这 ...
- Java SE (6)之 多线程
package com.sunzhiyan03; /* * 演示多线程 * */ public class Demo3 { public Demo3() { // TODO Auto-generate ...