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 4
//#include<cstdio>
//using namespace std;
//const int maxn=100000+10;
//int a[maxn];
//int main()
//{
// int n;
// int maxsum;
// int thissum;
// int start;
// int last;
// scanf("%d",&n);
// for(int i=0;i<n;i++)
// {
// scanf("%d",&a[i]);
// }
// maxsum=0;
// for(int i=0;i<n;i++)
// {
// thissum=0;
// for(int j=i;j<n;j++)
// {
// thissum+=a[j];
// if(thissum>=maxsum)
// {
// maxsum=thissum;
// start=i;
// last=j;
// }
// }
// }
// if(maxsum>=0)
// printf("%d %d %d",maxsum,a[start],a[last]);
// else
// printf("0 0 %d",n-1);
// return 0;
//}
#include<cstdio>
using namespace std;
const int maxn=100000+10;
int a[maxn];
int main()
{
int n;
int flag=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>=0)//最大值为0
flag=1;
}
int maxsum,thissum;
int start,last;
start=last=0;
maxsum=thissum=0;
int s=0;
for(int i=0;i<n;i++)
{
thissum+=a[i];
if(thissum>maxsum||(thissum==maxsum&&maxsum==0))
{
maxsum=thissum;
last=i;
start=s;
}
if(thissum<0)
{
thissum=0;
s=i+1;
}
}
if(flag==1)
printf("%d %d %d",maxsum,a[start],a[last]);
else
printf("0 %d %d",a[0],a[n-1]);//样例全是负数
return 0;
}

  

01-复杂度2. Maximum Subsequence Sum (25)的更多相关文章

  1. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  2. 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 ...

  3. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

  4. 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  5. 01-复杂度2 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  6. 01-复杂度2 Maximum Subsequence Sum

    01-复杂度2 Maximum Subsequence Sum   (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...

  7. 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 ...

  8. 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 ...

  9. pat1007. Maximum Subsequence Sum (25)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

随机推荐

  1. [springMVC]javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean

    问题描述: 页面使用标签<form:form>进行提交时,出现[springMVC]javax.servlet.jsp.JspTagException: Neither BindingRe ...

  2. Android 监听器

    Android提供很多种事件监听器,监听器主要是为了相应某个动作,可以通过监控这种动作行为,来完成我们需要的程序功能.      OnItemClickListener:               ...

  3. J2SE知识点摘记(九)

    1.         线程操作的一些方法 方法名称              方法说明 public static int activeCount()             返回线程组中目前活动的线 ...

  4. Genymotion中SD卡目录在Eclipse中查看,以及创建SDCard

    咦?这后面似乎指向一个目录,我就去找/mnt/shell/emulated/0 Wow~好熟悉的目录..不熟悉的同学可以打开android模拟器的File Manger App 里面就是这些目录了,然 ...

  5. 数据库CRUD操作

    CRUD操作: C:create 增加数据: insert into 表名 values('N001','汉族') 普通 insert into 表名 values('','','') 如果有自增长列 ...

  6. opencv 图像修复函数

    void cv::inpaint( const Mat& src, const Mat& mask, Mat& dst, double inpaintRange, int fl ...

  7. MySQL加强

    MySQL加强 Default Not null Unique Primary key Zerofill primary key auto_increment primary key auto_inc ...

  8. Day01

    1.@Test函数,执行后控制台没有输出结果? 1)  不能用静态方法,控制台会没有结果. 2)  不能把类名命名为Test,@Test不识别. 2.遍历Map集合的entrySet方法不会? 3.使 ...

  9. Python之美[从菜鸟到高手]--生成器之全景分析

    yield指令,可以暂停一个函数并返回中间结果.使用该指令的函数将保存执行环境,并且在必要时恢复. 生成器比迭代器更加强大也更加复杂,需要花点功夫好好理解贯通. 看下面一段代码: def gen(): ...

  10. 【leetcode边做边学】二分查找应用

    很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...