1007. Maximum Subsequence Sum (25)
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 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 题本身不难,但是想了好久竟然没有思路... 也是在看了别人的代码后才写出来通过的
遍历两边,一边遍历一边记录下最大的和以及开始和结束的数字。
出了一次问题,就是当序列中最大的值是0的时候怎样正确输出 0 0 0
#include <iostream> using namespace std; int a[]; int main()
{
int temp,k,start,end1,sum;
sum=-;
cin>>k;
for(int i=;i<k;i++){
cin>>a[i];
}
for(int i=;i<k;i++){
temp=;
for(int j=i;j<k;j++){
temp=temp+a[j];
if(temp>sum){
sum=temp;
start=a[i];
end1=a[j];
}
}
}
if(sum<=-)
cout<<<<" "<<a[]<<" "<<a[k-];
else
cout<<sum<<" "<<start<<" "<<end1; return ;
}
1007. Maximum Subsequence Sum (25)的更多相关文章
- 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 ...
- 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 ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 连续子数组的最大和/1007. Maximum Subsequence Sum (25)
题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...
- 1007 Maximum Subsequence Sum (25)(25 point(s))
problem Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is define ...
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
随机推荐
- SQL 一条SQL语句 统计 各班总人数,男女各总人数 ,各自 男女 比例 (转)
select sClass 班级,count(*) 班级学生总人数, sum(case when sGender=0 then 1 else 0 end) 女生人数, sum(case when ...
- Transaction Save Point (SET XACT_ABORT { ON | OFF })
ref:http://blog.csdn.net/wym3587/article/details/6940630 ref:http://www.cnblogs.com/jiajiayuan/archi ...
- 鸟哥linux私房菜基础篇
1)注销:exit2)指令太长:命令太长的时候,可以使用反斜杠 (\) 来跳脱[Enter]符号,使挃令连续到下一行3)系统语言显示和设置命令:echo $LANG,显示当前系统语言:简体中文zh_C ...
- 关于ES6的 模块功能 Module 中export import的用法和注意之处
export default 的用法 export default命令用于指定模块的默认输出.显然,一个模块只能有一个默认输出,因此export deault命令只能使用一次.所以,import命令后 ...
- Extjs 一些配置以及方法
1.例如想要实现以下功能,本来model中只有用户的firstname和lastname,但是在grid中展示还需要展示用户姓名,或者只展示用户姓名
- html5,加密元素
<form action=""> 账号:<input type="text" name="user"> ...
- 在Excel表格中输入一大段文字
1.有时为了注释的需要,在excel中需要输入一大段文字,这时候可以使用—视图-工具-绘图,然后选择下面的文本框,即可自定义文本框大小,如需要文本框和表格边框完全重合,在鼠标画文本框时按住 Alt键.
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- Linux命令的返回码列表
转自:http://blog.chinaunix.net/uid-10347480-id-3263127.html 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序 ...
- JVM基础知识
JVM简介 JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.J ...