Maximum Subsequence Sum
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<iostream>
#include<cstdio>
using namespace std; const int MAXN=;
const int INF=0x7f7f7f7f;
int k,x,y,maxn=-INF;
int a[MAXN],f[MAXN]; int main()
{
scanf("%d",&k);
for(int i=;i<=k;i++)
scanf("%d",&a[i]);
f[]=-;
for(int i=;i<=k;i++)
{
f[i]=max(f[i-]+a[i],a[i]);
if(f[i]>maxn)
y=i,maxn=f[i];
}
for(x=y;x>=;x--)
if(f[x-]<) break;
if(maxn<)
printf("0 %d %d",a[],a[k]);
else
printf("%d %d %d",maxn,a[x],a[y]);
return ;
}
Maximum Subsequence Sum的更多相关文章
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
随机推荐
- 多线程篇四:ThreadLocal实现线程范围内变量共享
1.static实现线程范围内变量共享 package com.test.shareData; import java.util.Random; /** * 多线程范围内的数据共享 * @author ...
- oracle相关常识
1.数据类型 VARCHAR2() NUMBER() DATE CLOB BLOB 2.复制表:create table tableName as select * from emp3.新增列:ALT ...
- Django的模型层
一.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...
- scss-@if指令
@if指令接受表达式和使用嵌套样式,无论表达式的结果只不过是false或null. 语法: @if expression { //CSS codes are written here } scss实例 ...
- 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式
一.概述. 在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架 ...
- Linux漏洞分析入门笔记-Off-By-One(栈)
ubuntu-16.04.5(X86) IDA7.0 0x00.漏洞描述 1.什么是off by one?又称1字节溢出. 源字符串长度等于目标缓冲区长度时,将源字符串复制到目标缓冲区可能会导致off ...
- matlab中repmat函数的用法(堆叠矩阵)
matlab中repmat函数的用法 B = repmat(A,m,n) B = repmat(A,[m n]) B = repmat(A,[m n p...]) 这是一个处理大矩阵且内容有重复时使用 ...
- Siebel学习笔记
Siebel(escript)的学习:1.Siebel的数据类型 Primitive(原始的)---Number,Integer,Hexadecimal(十六进制),Octal(八进制),Floati ...
- win10安装mxnet cuda9.0-gpu版
类似于上一篇文章 Anaconda3.6 python3.6.5 cuda9.0+ cudnn7.0 安装MXNet 1.设置清华源 conda config --prepend cha ...
- mysql在表的某一位置增加一列、删除一列、修改列名
如果想在一个已经建好的表中添加一列,可以用以下代码: ) not null; 这条语句会向已有的表中加入一列,这一列在表的最后一列位置.如果我们希望添加在指定的一列,可以用: ) not null a ...