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. fineuploader php服务端

    新版本的fineuploader项目中已经没有现成的php server端,需要额外使用composer进行安装,折腾好久才下下来,在此分享出来给大家. 下载地址: http://files.cnbl ...

  2. Django聚合查询 orm字段及属性

    目录 一 聚合查询 1. 级联 级联删除 级联更新 2. 聚合函数 使用 aggregate 使用场景 3. 分组查询 语法 使用 annotate 代码 4. F与Q查询 F查询 Q查询 二 ORM ...

  3. 异常日志文件errorlong

    #region log ////////////////////use/////////////// /// <summary> /// 异常日志 /// </summary> ...

  4. JS正则表达式语法(含ES6)(表格简要总结)

    文章目录 JS正则表达式 1. JS中正则表达式定义 2. 直接量字符 3. 字符类 4. 重复字符 5. 选择,分组和引用 6. 指定匹配位置 7. 修饰符 8. String 方法 9. RegE ...

  5. YiluPHP是如何做到不用配置、不用注入就能直接使用所有的类?

    使用过YiluPHP的人都会发现,不管是模型类还是逻辑类.辅助类还是工具类,使用所有类都不需要在配置文件中设置加载或注入,也不需要在页面中使用 include 或 require 或 use ,直接使 ...

  6. python连接数据库使用SQLAlchemy

    参考python核心编程 ORM(Object Relational Mapper),如果你是一个更愿意操作Python对象而不是SQL查询的程序员,并且仍然希望使用关系型数据库作为你的后端,那么你可 ...

  7. 因特尔CPU上TM和R标识的区别

    TM是英文trademark的缩写,TM标志并非对商标起到保护作用,它与R不同,TM表示的是该商标已经向国家商标局提出申请,并且国家商标局也已经下发了<受理通知书>,进入了异议期,这样就可 ...

  8. Shell(七):文件包含

    和其他语言一样,Shell 也可以包含外部脚本(类似python中import的功能).这样可以很方便的封装一些公用的代码作为一个独立的文件. Shell 文件包含的语法格式如下: . filenam ...

  9. 可编程实验板EPM1270T144C5使用说明

  10. weed3-2.2.细讲插入和更新

    Weed3 一个微型ORM框架(只有0.1Mb哦) 源码:https://github.com/noear/weed3 源码:https://gitee.com/noear/weed3 这篇重点讲讲插 ...