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 thelargest 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 themaximum 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
 
题目意思:求最⼤连续⼦序列和,输出最⼤的和以及这个⼦序列的开始值和结束值。如果所有数都⼩于0,那么认为最⼤的和为0,并且输出⾸尾元素。
解题思路:最开始的思路就是直接两层循环,设置i和j两个指针直接来定位求和,但是后来发现,其实可以直接使用一层循环,因为所求的和一般情况下必然是正数,除非全都是负数。但如果一开始就将全是负数的情况剔除后,只用一层循环便可以,一旦求和的结果是负数,那么起始定位的指针i便从其后重新取。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x7fffffff
using namespace std; int a[];
int main()
{
int n,i,left,right,ans=,l=;
int maxs=-inf;
int flag=;
scanf("%d",&n);
for(i=; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]>=)
{
flag=;
}
}
if(flag==)//全部为负数的情况
{
printf("%d %d %d\n",ans,a[],a[n]);
return ;
}
for(i=; i<=n; i++)
{
ans+=a[i];
if(ans<)
{
ans=;
l=i+;
}//直到连续子序列出现正数
else if(ans>maxs)//更新最大连续子序列
{
maxs=ans;
left=l;
right=i;
}
}
printf("%d %d %d\n",maxs,a[left],a[right]);
return ;
}

PAT 1007 Maximum Subsequence Sum 最大连续子序列和的更多相关文章

  1. PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)

    Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...

  2. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

  3. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  4. PAT 1007 Maximum Subsequence Sum (25分)

    题目 Given a sequence of K integers { N​1​​ , N​2​​ , ..., N​K​​ }. A continuous subsequence is define ...

  5. [pat]1007 Maximum Subsequence Sum

    经典最大连续子序列,dp[0]=a[0],状态转移dp[i]=max(dp[i-1]+a[i],a[i])找到最大的dp[i]. 难点在于记录起点,这里同样利用动态规划s[i],如果dp[i]选择的是 ...

  6. 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 ...

  7. 1007 Maximum Subsequence Sum (PAT(Advance))

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  8. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  9. 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 ...

随机推荐

  1. 将数据库中数据导出为excel表格

    public class Excel { private static Logger logger = LoggerFactory.getLogger(Excel.class); /** * 导出项目 ...

  2. SSM(Spring+SpringMVC+Mybatis)框架整合

    1.数据准备 SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `admin` -- - ...

  3. WPF的DataGrid用法-小白向

    前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功能实现所折磨.网络上的解决方法太多,但也太杂.没法子,我只好硬着头皮阅览各种文献资料,然后不断的去尝试,总算小有成果 ...

  4. OpenSSL 自述

    1995 年, Eric A. Young 和 Tim J. Hudson 发明了 SSLeay,它是 SSL(Open-source Secure Sockets) 协议的实现.1998 年,You ...

  5. python的tqdm模块介绍

    https://www.jianshu.com/p/b27318efdb7b Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator) ...

  6. MongoDB(七):聚合aggregate

    1. 聚合aggregate 聚合主要用于计算数据,类似sql中的sum().avg() 语法: db.集合名称.aggregate([{管道:{表达式}}]) stu准备的数据: db.stu.in ...

  7. AE单词备忘

    类的基本特性内 approved 已批准 implemented 已实施 mandatory 强制性的 proposed 偍仪的 validated 已验证

  8. JavaWeb中实现通过邮箱找回密码

    在开发JavaWeb项目中,利用邮箱帮用户找回密码.效果展示:   需要一个发送邮件的jar包 : javax.mail .jar1.JSP页面(设置邮箱输入框) HTML: <p >请输 ...

  9. Oracle trunc函数的使用

    1. 对日期的操作 2. 对数字的操作 1.对日期的操作 /**************日期********************/ SELECT TRUNC(SYSDATE) FROM DUAL; ...

  10. Java面向对象之继承(一)

    目录 Java面向对象之继承 引言 继承的特点 语法格式 父子类的关系 继承要点 重写父类方法 继承中的构造器 继承中的super关键字 ... Java面向对象之继承 继承是面向对象的第二大特征,是 ...