Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1. 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 (≤). 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>
const int maxn = ; int main()
{
int a[maxn];
int dp[maxn];
int s[maxn]; int n;
bool flag = false;
scanf("%d",&n);
for ( int i = ; i < n; i++ )
{
scanf("%d",&a[i]);
if ( !flag && ( a[i] >= ) )
{
flag = true;
}
} if ( !flag )
{
printf("0 %d %d",a[],a[n-]);
}
else
{
dp[] = a[];
s[] = ;
for ( int i = ; i < n; i++ )
{
if ( dp[i - ] + a[i] >= a[i] )
{
dp[i] = dp[i - ] + a[i];
s[i] = s[i - ];
}
else
{
dp[i] = a[i];
s[i] = i;
}
} int k = -;
int max = -;
for ( int i = ; i < n; i++)
{
if ( dp[i] > max)
{
max = dp[i];
k = i;
}
} printf("%d %d %d",max,a[s[k]],a[k]);
}
return ;
}

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. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  5. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

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

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

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

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

  8. 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)

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

  9. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

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

随机推荐

  1. sql servse 常用维护sql

    1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...

  2. 从香农熵到手推KL散度

    信息论与信息熵是 AI 或机器学习中非常重要的概念,我们经常需要使用它的关键思想来描述概率分布或者量化概率分布之间的相似性.在本文中,我们从最基本的自信息和信息熵到交叉熵讨论了信息论的基础,再由最大似 ...

  3. Matlab适配器模式

    适配器模式是连接两个不兼容接口的桥梁,主要分为三种:类适配器.对象适配器以及接口适配器,本文根据https://blog.csdn.net/u012359453/article/details/791 ...

  4. Gson含抽象类的反序列化

    Gson含抽象类的反序列化 场景描述: 反序列化A类的时候,这个类里面有一个抽象类属性B,B的实现类C里面又有一个抽象类属性D,D的实现类是E 实体类准备 public class A impleme ...

  5. Shell脚本编程30分钟入门(小结)(转)

    原文链接https://www.jb51.net/article/161028.htm 什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿.程序化脚本,是一种 ...

  6. 新建本地用户连接vsftp出现530 Login incorrect

    新建的用户的方式 [root@centos2 /var/ftp]# useradd -s /sbin/nologin user1 出错原因: /etc/pam.d/vsftp文件作了限制 [root@ ...

  7. vue2 design 手记

    Ant Design of Vue github地址:https://github.com/vueComponent/ant-design-vue Ant Design of Vue文档:https: ...

  8. 分布式存储-ceph

    1. ceph 简介 Ceph是一种为优秀的性能.可靠性和可扩展性而设计的统一的.分布式文件系统().ceph 的统一体现在可以提供文件系统.块存储和对象存储,分布式体现在可以动态扩展.在国内一些公司 ...

  9. SQL进阶系列之10HAVING子句又回来了

    写在前面 HAVING子句的处理对象是集合而不是记录 各队,全队点名 --各队,全体点名! CREATE TABLE Teams (member CHAR(12) NOT NULL PRIMARY K ...

  10. C# 退出应用程序的几种方法

    Application.Exit();//好像只在主线程可以起作用,而且当有线程,或是阻塞方法的情况下,很容易失灵 this.Close();//只是关闭当前窗体. Application.ExitT ...