01-复杂度2 Maximum Subsequence Sum   (25分)

  • 时间限制:200ms
  • 内存限制:64MB
  • 代码长度限制:16kB
  • 判题程序:系统默认
  • 作者:陈越
  • 单位:浙江大学

https://pta.patest.cn/pta/test/3512/exam/4/question/58272

Given a sequence of KKK integers { N1N_1N​1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i+1}N​i+1​​, ..., NjN_jN​j​​ } where 1≤i≤j≤K1 \le i \le j \le K1≤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 KKK (≤10000\le 10000≤10000). The second line contains KKK 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 iii and jjj (as shown by the sample case). If all the KKK 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; int main()
{
int k,x,maxSum=-,sum=,s=,st=,e;
cin>>k;
for(int i=;i<k;i++)
{
cin>>x;
if(!i)
s=x;
if(sum<||!i)
{
sum=;
st=x;
}
sum+=x;
if(sum>maxSum)
{
maxSum=sum;
s=st;
e=x;
}
}
if(maxSum<)
{
maxSum=;
e=x;
}
cout<<maxSum<<' '<<s<<' '<<e;
return ;
}

01-复杂度2 Maximum Subsequence Sum的更多相关文章

  1. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  2. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

  3. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

  4. 最大子列和CT 01-复杂度2 Maximum Subsequence Sum

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

  5. 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

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

  6. 01-复杂度2. Maximum Subsequence Sum (25)

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

  7. 01-复杂度2 Maximum Subsequence Sum (25 分)

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

  8. 01-复杂度2 Maximum Subsequence Sum (25 分)

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

  9. Maximum Subsequence Sum 最大子序列和的进击之路

    本文解决最大子序列和问题,有两个题目组成,第二个题目比第一个要求多一些(其实就是要求输出子序列首尾元素). 01-复杂度1 最大子列和问题   (20分) 给定KK个整数组成的序列{ N1​​, N2 ...

随机推荐

  1. (一)vim插件tabbar

    今天早晨给 TabBar.vim 的作者Groleo发了个邮件询问这个tabbar关闭失败的问题.得到的答复是 "you can use :bd instead of :close" ...

  2. DG磁盘分区提示错误

    文章更新于:2020-02-26 一.当你使用 DG 分区遇到错误时 1.错误复现 2.解决办法 以管理员身份打开cmd 运行 chkdsk /f /x g:(这里的 g 替换成你要检查的盘符) 然后 ...

  3. django发送邮件的坑

    django发送邮件的坑 配置django发邮件的时候本地发送时好好的,但是放到阿里云的服务器上却不能发送. 经过一系列的排查后终于发现是阿里云把25端口给封了. 后来改用smtps的方式发送,更改d ...

  4. 【摸鱼向】UE4的AI模块探索手记(1)

    前言 之前实现了自主创作的角色导入进UE4并成功控制其进行一系列动作,但目前的样子距离基本的游戏架构还差了一个很大的模块:NPC,而这部分是由电脑来进行自动控制,所以,我有一句话不知当讲不当讲(对,我 ...

  5. String 对象-->lastIndexOf() 方法

    1.定义和用法 lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,如果指定第二个参数 start,则在一个字符串中的指定位置从后向前搜索. 语法: string.lastInde ...

  6. String 对象-->fromCharCode() 方法

    1.定义和用法 将ASCII码转换成对应的字符 语法: String.fromCharCode(n1, n2, ..., nX) 参数: n1, n2, ..., nX:一个或多个 Unicode 值 ...

  7. stand up meeting 1-6

    今日更新: 1.修复初始最佳战绩显示bug:  初始为击败全国0% 用户 2.挑战结果界面显示“哎,今天的饭又白吃了,回去多吃两碗###”, 去除API返回string中的“###”. 3.分享模块初 ...

  8. B - Red and Black 直接BFS+队列

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  9. linux通过进程名查看其占用端口

    1.先查看进程pid ps -ef | grep 进程名 2.通过pid查看占用端口 netstat -nap | grep 进程pid 参考: https://blog.csdn.net/sinat ...

  10. Vue定义全局过滤器filter

    这里介绍的是多个过滤器一起添加到全局中 1.创建方法 首先src下新建plugin文件夹,用来存放插件. 在plugin文件夹内新建filters.js,编写方法(如隐藏手机号码等等...) /** ...