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. (转).Net Core控制台生成exe能独立运行

    原文介绍了两种方式,方式一经测试可用(生成exe在开发机器上可运行),但是因为服务器是windows server2012 r2,没有安装补丁,造成了困难,尚未在服务器上运行成功. (提示 api-m ...

  2. tf.tile()函数的用法

    y = tf.tile(tf.range(2, dtype=tf.int32)[:, tf.newaxis], [2,3]) # tf.tile(input,[a,b]) 输入数据,按照对应维度将矩阵 ...

  3. LocalStorageUtils

    对localStorage进行封装: var LocalStorageUtils = new function (){ if(window.localStorage==null){ throw new ...

  4. INTERVAL 用法 mysql

    原文:https://blog.csdn.net/sqlquan/article/details/82699237 做个例子描述吧,也许更易于理解. 准备: 1.建表 create table INT ...

  5. 网络编程之 tcp服务器(一)

    1.创建套接字 2.bind绑定ip和port 作为服务方,ip port 应该是固定的,所以要绑定;客户端一般不绑定 3.listen使套接字变成监听套接字,即变为被动链接 4.accept等待客户 ...

  6. Nginx proxy_set_header

    配置示例 server{ server_name aaa.com location /api { proxy_pass http://xxx.com/api; proxy_set_header Hos ...

  7. sublime text3常用的一些快捷键

    --------------------------------下面的内容可以打印出来贴在电脑旁提醒自己-------------------- Ctrl + Shift + D  快速复制当前的一行 ...

  8. WinServer-the security database on the server does not have a computer account for

    用了本地的administrator登陆

  9. 外部服务发现-ingress

    Ingress`其实就是从 kuberenets 集群外部访问集群的一个入口,将外部的请求转发到集群内不同的 Service 上,其实就相当于 nginx.haproxy 等负载均衡代理服务器,Ing ...

  10. RedHat7.4 yum配置

    RedHat7.4 yum配置 linux 1. yum配置 1.1 本地yum源配置 1.2 配置网络yum源为CentOS源 1. yum配置 1.1 本地yum源配置 设置使用ISO镜像软件:虚 ...