01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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 a[maxn] = {};
int dp[maxn] = {};
int s[maxn] = {}; int main(){
int n;
scanf("%d",&n);
bool flag = false;
for(int i = ; i < n; i++){
scanf("%d",&a[i]);
if(a[i] >= ) flag = true;
}
if(!flag){
printf("0 %d %d",a[],a[n-]);
return ;
}
//scanf("%d",&n);
dp[] = a[];
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 max = dp[];
int k = ;
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 分)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- 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 ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
随机推荐
- [SoapUI] SOAP UI-Groovy Useful Commands
Hi All, I have posted the SOAPUI and Groovy useful commands that may help you in your testing. Below ...
- [GO]并发的网络爬虫
package main import ( "fmt" "strconv" "net/http" "os" " ...
- [GO]结构体的比较和赋值
package main import "fmt" func main() { type student struct { id int name string sex byte ...
- win7设置开机启动virtualBOX虚拟机
如果常用VirtualBox虚拟机系统的话,设置随开机启动也是很方便的.不需要打开VirtualBox窗口,直接启动VirtualBox虚拟机系统就可以了. 设置开机自启动VirtualBox虚拟机系 ...
- 编写高质量代码改善C#程序的157个建议——建议75:警惕线程不会立即启动
建议75:警惕线程不会立即启动 现代的大多数操作系统都不是一个实时的操作系统,Windows系统也是如此.所以,不能奢望我们的线程能够立即启动.Windows内部会实现特殊的算法以进行线程之间的调度, ...
- WebService搭建
好久没有用WebService了,今天想复习一下原来的技术,结果错误百出. 这几天重装了系统,所有的东西都要重新配置,导致了很多原来没有发生过的问题,在这里做个笔记,希望以后不会再有这样的错误.
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...
- Mysql简介与编译安装
==========MYSQL工作原理图: 1>数据库简介:简单的说数据库(database)就是一个存储数据的仓库,它将数据按照特定的规律存储到磁盘上,通过数据库管理系统,能够有效的管理存储在 ...
- opencv——通过面积筛选最大轮廓,并求凸包矩形的长和宽
#include "stdafx.h" #include <iostream> #include<string> #include <stdio.h& ...
- 文字编码ASCII,GB2312,GBK,GB18030,UNICODE,UCS,UTF的解析
众所周知,一个文字从输入到显示到存储是有一个固定过程的,其过程为:输入码(根据输入法不同而不同)→机内码(根据语言环境不同而不同,不同的系统语言编码也不一样)→字型码(根据不同的字体而不同)→存储码( ...