problem

1007 Maximum Subsequence Sum (25分)
Given a sequence of K integers { N
​1
​​ , N
​2
​​ , …, N
​K
​​ }. A continuous subsequence is defined to be { N
​i
​​ , N
​i+1
​​ , …, N
​j
​​ } 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

  • 给定一个长为n的序列
  • 求最大子序列和,以及该序列的开始和结束值。

solution

  • 求最大和:tmp为到第i个数时(包含第i个数)的最大字段和,转移时直接考虑加上第i+1个数是大了还是小了(更新全局最大字段和sum,已覆盖所有状态),转移后tmp加上第i+1个数继续往下走(因为必须连续),若是负数则直接断开,前面后面分别求。
  • 求序列区间:left和right为开始值和结束值,在每次更新tmp和sum的时候更新对应的区间即可。
#include<iostream>
#include<string>
using namespace std;
int a[10010];
int main(){int n; cin>>n;int sum = -1, tmp = 0, tmpx = 1;int left = 1, right = n;for(int i = 1; i <= n; i++){cin>>a[i];tmp += a[i];if(tmp < 0){tmp = 0;tmpx = i+1;}else if(tmp > sum){sum = tmp;left = tmpx;right = i;}}if(sum<0)sum = 0;cout<<sum<<" "<<a[left]<<" "<<a[right];return 0;
}

【PAT甲】1007 Maximum Subsequence Sum (25分),求最大字段和及区间相关推荐

  1. PAT甲级--1007 Maximum Subsequence Sum (25 分)

    题目详情 - 1007 Maximum Subsequence Sum (25 分) (pintia.cn) Given a sequence of K integers { N1​, N2​, .. ...

  2. PAT甲级 -- 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  3. PAT甲级1007 Maximum Subsequence Sum (25 分)题解

    \quad 这是最大子序列和问题,是一个典型的动态规划题目,我在博客中对几个常见的动态规划算法进行了分析.设输入的数保存在数组a[max]中,那么转移方程为dp[i]=max(dp[i-1]+a[i] ...

  4. 【测试点5】1007 Maximum Subsequence Sum (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a sequence of K integers { N​1​​ , N​2​​ , -, N​K​​ }. A co ...

  5. 1007 Maximum Subsequence Sum (25 分) java 题解

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

  6. PAT A 1007. Maximum Subsequence Sum (25)

    原题 Given a sequence of K integers { N1, N2, ..., NK }.  A continuous subsequence is defined to be { ...

  7. 【PAT甲级 最长公共子串】1007 Maximum Subsequence Sum (25 分) C++ 全部AC

    题目 读题!读题! 题上让输出最长子串起始位置和终止位置的数字,而不是角标 如果每个数都是负数,则输出0 不要看着测试用例去猜题意,要先把题完整看完了再开始码! 如果自己的测试用例全都通过了,题目的测 ...

  8. 1007 Maximum Subsequence Sum (25 分)【难度: 一般 / 知识点: 最大子序列和】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 方法一: 前缀和+枚举 时间复杂度: O(n ...

  9. 【个人刷题笔记】1007 Maximum Subsequence Sum (25 分)最大子段和简单DP,一堆坑

    思路: 思路简单的,简单的dp,我是采取了一个前缀和用于计算区间的总和 ( sum[i + k] - sum[i - 1] 可以表示区间[i , i + k] 的总和) 一看就会,一写就废 首先是第一 ...

最新文章

  1. 什么是java四大引用?
  2. class与case class的区别(转载)
  3. C# 读写ini配置文件demo
  4. android读写相册权限,androidQ 关于存储权限相册图片
  5. 吴恩达神经网络和深度学习-学习笔记-18-Softmax回归
  6. IP数据库的生成与使用
  7. android 沙盒双开 微信,微信双开回来了,而且还不会被封号
  8. JQuery文件上传插件Uploadify
  9. 2017年5月20日软考考试报名开始啦
  10. 怎么把图片的边缘弄圆_如何PS制做出边缘清晰或虚化的圆角照片
  11. 在火狐浏览器打开xpath_元素定位工具:火狐浏览器Try Xpath插件
  12. WWW2021 Interpreting and Unifying Graph Neural Networks with An Optimization Framework 阅读笔记
  13. 从今以后我一个人唱悲伤情歌:伤感的QQ空间日志
  14. 《视觉SLAM进阶:从零开始手写VIO》(二)
  15. 视频教程-2019 react入门至高阶实战,含react hooks-ReactJS
  16. 细细品味Hadoop_Hadoop集群精华文档合集
  17. 设计求任意两个整数和的web程序,用户通过提交页面(input.jsp)输入两个整数,并提交给一个(sum.jsp)程序
  18. OpenCV学习笔记(5)_ ellipse绘制函数浅析
  19. 弘辽科技:淘宝提升销量改价可以吗?改价有影响吗?
  20. 2023最新微信群裂变推广分享加群HTML源码/纯开源/UI简约大气

热门文章

  1. 标准模板库(STL) map —— 初始化问题
  2. C++11 —— 基于区间(range)的 for 循环
  3. utilities(matlab)—— 前馈网络权值矩阵初始化方式
  4. Spark 机器学习 —— 从决策树到随机森林
  5. Python 进阶 —— 使用 map reduce
  6. 机器学习基础(十六)—— bootstrap
  7. Python 基础 —— sorted
  8. 矩阵等式 matrix identity(numpy仿真)
  9. 产生一定范围随机数的通用表达式
  10. forward计算机语言,const_forward在C中的可选实现中做了什么?