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. jfinal shiro共享

    和上一篇tomcat sexxion共享一样,用的也是redis 代码: package com.test.shiro; import com.jfinal.log.Log; import com.j ...

  2. 聊一聊 Vue 中 watch 对象中的回调函数为什么不能是箭头函数?

    聊一聊 Vue 中 watch 对象中的回调函数为什么不能是箭头函数 本文重点知识点速览: Vue 中的 watch 对象中的回调函数不能是箭头函数. 箭头函数中的 this 指向的是函数定义时所在的 ...

  3. vue 中 px转vw的用法

    下面介绍最简单的用法 1 下载依赖 npm install postcss-import postcss-loader postcss-px-to-viewport --save-dev 2 在项目根 ...

  4. vue解惑之slot(插槽)

    一.插槽是个什么玩意,能吃吗 在vue中[插槽],从字面意思来看,插槽意味着[内容的增加],回到vue的使用场景,插槽就是[父组件调用子组件时,额外增加的内容]. 插槽显不显示.显示的内容是由父组件来 ...

  5. ArcGIS Runtime SDK for WPF学习笔记(一)

    本节主要讲解如何安装ArcGIS Runtime SDK,以及移除注释与水印. 附上ArcGIS Runtime SDK for .NET的官方操作手册网址:https://developers.ar ...

  6. Windows系统下解决PhPStudy MySQL启动失败

    报错 Apache\Nginx服务正常启动了,但是MySQL却一直启动失败. 解决流程 查看端口是否被占用 打开系统自带的资源管理器,查看监听端口3306是不是被占用,下图中3306端口被mysqld ...

  7. zabbix服务深入

    第1章 Grafana自定义图形 1.安装grafana [root@m01 /data/soft]# wget https://dl.grafana.com/oss/release/grafana- ...

  8. 重启docker服务应用,自启停命令.

    #重启docker服务应用,不自动开启docker容器 docker update --restart=no (docker容器CONTAINER ID 或 docekr容器NAMES) #重启doc ...

  9. go笔记--rpc和grpc使用

    目录 go笔记--rpc和grpc使用 rpc server.go client.go (sync) client.go (async) grpc protoc server.go client.go ...

  10. Dubbo学习系列之十四(Seata分布式事务方案AT模式)

    一直说写有关最新技术的文章,但前面似乎都有点偏了,只能说算主流技术,今天这个主题,我觉得应该名副其实.分布式微服务的深水区并不是单个微服务的设计,而是服务间的数据一致性问题!解决了这个问题,才算是把分 ...