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. Visual Studio 2019 安装

    目录 写在前面 官网下载 安装 等待安装 启动 写在前面 目前工作的开发环境还是旧版本的Visual Studio 2013版.个人感觉还是有点跟不上时代更新迭代的节奏了.毕竟,技术在进步.如果我们也 ...

  2. C#使用Autofac实现控制反转IoC和面向切面编程AOP

    Autofac是一个.net下非常优秀,性能非常好的IOC容器(.net下效率最高的容器),加上AOP简直是如虎添翼.Autofac的AOP是通过Castle(也是一个容器)项目的核心部分实现的,名为 ...

  3. P1361 小M的作物 (最大流)

    题目 P1361 小M的作物 解析 把\(A\)看做源点,把\(B\)看做汇点,先不考虑额外情况 显然,这是一种两者选其一的问题,我们选择一部分边割去,使这部分边的贡献最小,就是求最小割,我们求出了收 ...

  4. 采用__call__ 实现装饰器模式

    装饰器模式在实现中也是很常见的:比如手机贴膜,手机壳 都是为了给手机增加一些额外功能 增加耐操 装饰器模式的本质就是对对象二次包装,赋额外功能 __call__ __call__是python魔术方法 ...

  5. 直播知识-推流&拉流

    推流,指的是把采集阶段封包好的内容传输到服务器的过程.其实就是将现场的视频信号传到网络的过程.“推流”对网络要求比较高,如果网络不稳定,直播效果就会很差,观众观看直播时就会发生卡顿等现象,观看体验很是 ...

  6. 【RAC】 RAC For W2K8R2 安装--dbca创建数据库(七)

    [RAC] RAC For W2K8R2 安装--dbca创建数据库(七) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可 ...

  7. 使用awrsqrpt.sql查看执行计划demo

    SQL> @?/rdbms/admin/awrsqrpt.sql Current Instance ~~~~~~~~~~~~~~~~ DB Id DB Name Inst Num Instanc ...

  8. Github 上 10 个值得学习的 Springboot 开源项目

    Spring Boot 几乎继承了所有 Spring 框架的优点,同时还可以让项目的配置更简化.编码更简化.部署更方便.近两年受到很多开发者的追捧,也是火热的不行! 下面给大家整理了 10 个 Git ...

  9. 22.centos7基础学习与积累-008-系统调优及安全设置

    从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 1.关闭selinux功能: SELinux(Securety-EnhancedLi ...

  10. C++学习(2)—— 数据类型

    C++规定在创建一个变量或者常量的时候,必须指定出相应的数据类型,否则无法给变量分配内存 数据类型存在意义:给变量分配合适的内存空间 1. 整型 作用:整型变量表示的是整数类型的数据 C++中能够表示 ...