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 ...
随机推荐
- Java网络编程之流——流、过滤器、阅读器和书写器
Java的I/O建立于流(Stream)之上.输入流读取数据:输出流写入数据.所有的输出流都有相同的基本方法来写入数据,所有输入流也使用相同的基本方法来读取数据.在创建流之后,你通常可以忽略在读写时的 ...
- eclipse自动补全快捷键失效,sysout用不了!
好久没写Java代码了,使用新版Neon的Eclipse Java EE IDE开发时,自动补全各种失败,sysout也各种用不了, 开始还以为是电脑卡比呢,原来是版本的快捷键不同了,修改方法如下! ...
- UIPickerView滚轮选择器视图
//必须实现两个协议 //数据源协议必须实现的两个方法 //选取器的输出借口singlePicker,并在故事版中选择该选取器将dataSource和delegate拖入视图控制器与之关联 //@pr ...
- git config 配置
1. git config简介 我们知道config是配置的意思,那么git config命令就是对git进行一些配置.而配置一般都是写在配置文件里面,那么git的配置文件在哪里呢?互动一下,先问下大 ...
- html a标签包含a标签,浏览器的行为处理
a标签包含a标签 浏览器可能是为了避免a的转跳重复,所以禁止了a标签包含a标签,如何你的代码中有a标签包含a标签,那么浏览器将会重新编码外层a标签,取外层a标签与内层a标签的差集,加上外层a标签,并把 ...
- Install Mono on Linux
Debian, Ubuntu, and derivatives Add the Mono Project GPG signing key and the package repository to y ...
- MVC 中301永久重定向
public class PermanentRedirectResult : ViewResult { public string Url { get; set; } public Permanent ...
- [SQL入门级] 第一天 SQL初步
SQL语句分为以下三种类型: DML:Data Manipulation Language 数据操纵语言 INSERT UPDATE DELETE SELECT DDL:Data Definition ...
- linux sed
sed 命令 sed -i 's/3306/3308/g' my.cnf mysql # 同时替换两个文件
- win7 ins 30131 oracle 12c
Cause - Failed to access the temporary location. Action - Ensure that the current user has required ...