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. 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 (≤). The second line contains Knumbers, 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<bits/stdc++.h>
using namespace std;
typedef long long ll;int main(){int n;cin >> n;int num[n];int flag = 1;for(int i=0;i < n;i++) {cin >> num[i];if(num[i] >= 0)flag = 0;}if(flag){cout << 0<<" " << num[0] <<" "<< num[n-1];return 0;}int sum = 0;int maxsum = 0;int j=0,k=0;int movej=0;for(int i=0;i < n;i++){sum += num[i];if(sum < 0) {sum = 0;movej=i+1;}if(maxsum < sum){j = movej;k = i;maxsum = sum;}}if(num[j] < 0)num[j] = 0;if(num[k] < 0)num[k] = 0;cout << maxsum << " " << num[j] << " " << num[k];return 0;
}

看错输出,应该是输出数字的,我输出下标了,还过了2个数据点??

然后边界条件有个

3
-1 -1 0

应该是输出0 0 0

转载于:https://www.cnblogs.com/cunyusup/p/10680730.html

PAT 1007 Maximum Subsequence Sum相关推荐

  1. PAT甲级1007 Maximum Subsequence Sum :[C++题解]DP,最大子序列和、求最优的区间方案

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: dp题. 这道题糅合了两个知识点: dp求最值(区间之和) 动态求区间方案:区间之和相等的条件下:要求区间左端点最靠前,如果左端点相 ...

  2. 【PAT甲】1007 Maximum Subsequence Sum (25分),求最大字段和及区间

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

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

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

  4. PAT甲级真题-1007 Maximum Subsequence Sum详解优化

    1007 Maximum Subsequence Sum 题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/9948055 ...

  5. 1007 Maximum Subsequence Sum

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

  6. 1007 Maximum Subsequence Sum

    1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1​, N2​, ..., NK​ }. A continu ...

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

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

  8. 1007. Maximum Subsequence Sum (25)-PAT甲级真题(最大连续子序列和、动态规划dp)

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

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

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

最新文章

  1. python 多态_Python中的多态
  2. LiTAMIN2:一种超轻型LiDAR-SLAM(ICRA2021)
  3. 《团队——科学计算器代码设计规范》
  4. 设计模式学习笔记 1.介绍
  5. java method方法_Java Method.getTypeParameters方法代碼示例
  6. Java反射(Reflection)
  7. VMware虚拟机安装CentOS6.4、部署web项目全过程(设置固定IP、安装JDK、Tomcat、Redis、部署项目)...
  8. python字符串方法总结_python字符串使用方法总结
  9. sql经典题目(1)
  10. java 正则表达式 替换 html,java 正则表达式 替换 html
  11. 2020年不能启动win7_包装车间2020年大修正式启动
  12. Unity5.3官方VR教程重磅登场-系列4 VR中的用户界面
  13. httprunner 3.x学习10 - parameters 参数化
  14. H5案例分享-H5游戏跳跃类玩法分享
  15. apicloud mysql_apiCloud中的API对象
  16. 【2021 ICPC Gran Premio de Mexico 2da Fecha F】Flipped Factorization 题解
  17. 手机版python3h如何自制游戏_Python 飞机大战|10 分钟学会用 python 写游戏
  18. unity中2D飞机游戏手指触屏操作的问题
  19. Kotlin ListView设置Adapter
  20. 认识大蟒:Python 快速入门 | 周末送书

热门文章

  1. 2019CCPC-江西省赛(重现赛)- 感谢南昌大学
  2. python机器学习---1. K近邻算法
  3. c++ 检查缓冲大小与记录大小是否匹配_后端程序员不得不会的 Nginx 转发匹配规则...
  4. 安装deepin_国产系统Deepin深度不是华为笔记本也能安装
  5. HttpServlet概述及应用
  6. 【算法竞赛学习】数据分析达人赛3:汽车产品聚类分析
  7. 曼哈顿距离最小生成树
  8. shell函数日期之间的操作(日期转秒,日期间隔秒,日期间隔天)
  9. QT 文字字体和颜色设置
  10. jQuery.Deferred和Promise