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. 独立版Jexus

    一:下载资源包 把 jexus压缩包下载到linux临时文件夹中. cd /tmp wget linuxdot.net/down/jexus--x64.tar.gz 二,解压: tar -zxvf j ...

  2. Android 如何修改自动同步数据的默认开关 M

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  3. 对手机SD卡的一些操作

    首先要导入外包 log4j-1.2.16.jar 代码如下: package com.car273.util; import java.io.BufferedReader; import java.i ...

  4. SLC、eSLC、MLC、eMLC的区别

    SLC.eSLC.MLC.eMLC的区别 作为SSD主要元件的NAND闪存,我们经常见到的有SLC和MLC两种,甚至还细分出eSLC和eMLC等等,现在我们谈一下他们之间的区别.       SLC全 ...

  5. delphi 实现vip126发邮件

    本例是 TSimpleThread , TSimpleList, IdhttpEx 网页模拟(POST)的综合运用. Demo只写了发送,但亦可收取,详见源码. (此源码写于2年前,那时还写得不好,请 ...

  6. C语言入门(3)——对Hello World程序的解释

    上篇我们写了一个最简单的程序.这个简单的程序包含了很多重要的内容.本篇我们通过这个最简单的Hello World程序逐一讲解C语言程序的一些特点. 打开Visual Studio 2013 通过菜单- ...

  7. openStack use

    <1,project security> security groyps Security groups--> are sets of IP filter rules() that ...

  8. 面向对象程序设计-C++_课时13初始化列表

    构造函数设置成员初值方法有两种:一种是在函数体内赋值,另一种是采用初始化列表的形式. 初始化列表BETTER 函数体内赋值 类名::类名(形参1,形参2,...形参n) { 数据成员1=形参1; 数据 ...

  9. 2014-11-21 DHTMLX是什么

    什么是dhtmlx? dhtmlx是一套网页开发 的函式库,他提供了树状元件.数据方格组件.工具列等组件供开发 人员使用. dhtmlx组件是一个JavaScript 库,提供了一套完整的Ajax驱动 ...

  10. JavaScript知识(二)

    你要保守你心,胜过保守一切,因为一生的果效,是由心发出的.————O(∩_∩)O... ...O(∩_∩)O...老师因有事下午没来上课,今天就只把中午讲的知识总结一下.由于昨天只是讲了JavaScr ...