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 <= i <= j <= K. 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 (<= 10000). 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>
//using namespace std;
//const int maxn=100000+10;
//int a[maxn];
//int main()
//{
// int n;
// int maxsum;
// int thissum;
// int start;
// int last;
// scanf("%d",&n);
// for(int i=0;i<n;i++)
// {
// scanf("%d",&a[i]);
// }
// maxsum=0;
// for(int i=0;i<n;i++)
// {
// thissum=0;
// for(int j=i;j<n;j++)
// {
// thissum+=a[j];
// if(thissum>=maxsum)
// {
// maxsum=thissum;
// start=i;
// last=j;
// }
// }
// }
// if(maxsum>=0)
// printf("%d %d %d",maxsum,a[start],a[last]);
// else
// printf("0 0 %d",n-1);
// return 0;
//}
#include<cstdio>
using namespace std;
const int maxn=100000+10;
int a[maxn];
int main()
{
int n;
int flag=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>=0)//最大值为0
flag=1;
}
int maxsum,thissum;
int start,last;
start=last=0;
maxsum=thissum=0;
int s=0;
for(int i=0;i<n;i++)
{
thissum+=a[i];
if(thissum>maxsum||(thissum==maxsum&&maxsum==0))
{
maxsum=thissum;
last=i;
start=s;
}
if(thissum<0)
{
thissum=0;
s=i+1;
}
}
if(flag==1)
printf("%d %d %d",maxsum,a[start],a[last]);
else
printf("0 %d %d",a[0],a[n-1]);//样例全是负数
return 0;
}
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 be { Ni, ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 01-复杂度2 Maximum Subsequence Sum
01-复杂度2 Maximum Subsequence Sum (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...
- 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 ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
随机推荐
- Android 原生listview item伸展收缩效果
Android原生listview做的一个item的伸缩效果.*永远不要让你老大有机会改需求 package com.example.yunkanglast; import java.io.Seria ...
- Bower —— 一个Web的包管理工具
作者:江剑锋 github地址:https://github.com/bower/bower Bower为何物 Bower是一个Web开发的包管理软件.前端开发中,或多或少,都会以来于现成的fra ...
- Javascript 缓冲运动——逐行分析代码,让你轻松了解缓冲运动的原理
看过上一篇关于Javascript 匀速运动文章的朋友相信对于运动已经有了初步的了解 接下来 讲一下关于缓冲运动的原理 ,我会逐行分析代码,代码简单易懂,能马上理解其中的原理,适用于初学者. #div ...
- jquery 选择器之children与find
children()和find() 两个用于向下遍历 DOM 树的 jQuery 方法 children() 方法返回被选元素的所有直接子元素. find() 方法返回被选元素的后代元素,一路向下直到 ...
- flex4 日期类型字符串转日期类型(string转Date)(转)
mysql数据库中存储的日期类型通过PHP返回到flex端为字符串类型,这样在flex中进行处理时就必须要将字符串转化为Date类型.如果仅仅是 "年/月/日" 的组合,而没有涉及 ...
- centos6.5 openvpn安装配置
http://m.jb51.net/?host=www.jb51.net&src=http%3A%2F%2Fwww.jb51.net%2Fsoftjc%2F150885.html
- #include <iostream>
1 static_assert 2 std::nothrow 3 std::ref() 4 std::string 1 static_assert 执行编译时断言检查 语法 static_assert ...
- 【MongoDB数据库】MongoDB 命令入门初探
MongoDB是一款NoSql数据库,使用了"面向集合"(Collection-Oriented)原理,意思是数据被分组存储在数据集中,被称为一个集合(Collection).每一 ...
- prototype演变
setp1 var Person = function () {}; //构造器 var p = new Person(); setp1 演变: var Person = function () {} ...
- SPDY HTTP2.0
SPDY(读作“SPeeDY”)是Google开发的基于TCP的应用层协议,用以最小化网络延迟,提升网络速度,优化用户的网络使用体验.SPDY并不是一种用于替代HTTP的协议,而是对HTTP协议的增强 ...