Maximum Subsequence Sum(接上篇)
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
总之和上面一篇几乎一样,有几点小坑注意一下就好了:
1: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.
2.The numbers must be separated by one space, but there must be no extra space at the end of a line.
3.最后一点没提到,也就是我一直不能AC的原因:
当sequence中 全是非正数的情况,并不能归纳到 1 中,举个例子:sequence : -1 -2 -3 0 -5
out:0 0 0 而不是 0 -1 -5
code:
#include <iostream>
#include <vector>
using namespace std; int SubSum(vector<int>& v,int& m,int& n,bool& find){
int MaxSum = ,tmp = ,tmp_m = v[];//先移动tmp_m
m = n = v[];//当sequence比当前大再把m移到tmp_m处
for(int i=;i<v.size();++i){
tmp += v[i];
if(tmp > MaxSum){
find = true;
int tmp_max = MaxSum;//tm_max用来判断是否移动n
MaxSum = tmp;
m =tmp_m;
if(MaxSum > tmp_max)
n = v[i];
}
else if(tmp < ){
tmp = ;
tmp_m = v[i+];
}
}
int max = v[];
for(int i=;i<v.size();++i)
if(v[i]>max){
max =v[i];
find = true;
}
if(max == )
m = n =;
return MaxSum;
} int main()
{
int n,tmp,start,end;
bool find =false;//是否存在最大子队列
vector<int> v;
cin >> n;
for(int i=;i<n;++i){
cin >> tmp;
v.push_back(tmp);
}
n = SubSum(v,start,end,find);
if(find)
cout << n << ' ' << start << ' ' << end << endl;
else
cout << n << ' ' << v.front() << ' ' << v.back() << endl;
return ;
}
Maximum Subsequence Sum(接上篇)的更多相关文章
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
随机推荐
- 【C#】OOP之继承那点事
前言: 继承这点事,说多不多,说少不少,这里只描述了一些我认为的基础篇,望各位大神指教.本节参照了C#高级编程和Think in java对继承的描述,我个人认为OOP只是思想,故看明白一个就说通的, ...
- HipChat上传文件报未知错误解决方案
前言 HipChat是Atlassian公司的一款团队协作即时通讯工具,服务端为Linux(官方给的服务端就是一个虚拟机),在Windows.Linux.Android.IOS.Mac等平台都有客户端 ...
- 获取 Windows Phone 手机系统信息
wpf: <phone:PhoneApplicationPage x:Class="ABSystemInfo.MainPage" xmlns="http://sch ...
- VS如何显示行号
1.随便打开一个项目,可以看到代码框内并没有显示行号 2.选择“工具”-“选项”,打开后界面如下 3.选择文本编辑器,找到下图中的“行号”并勾选 4.行号可以显示了
- Oracle备份表结构和数据
--创建一份表结构 create table BASE_GOODSPAYMENT_SETTING_BAK as select * from BASE_GOODSPAYMENT_SETTING ; -- ...
- ECMall如何支持SSL连接邮件服务器的配置
首先,主要是ecmall使用的phpmailer版本太低,不支持加密连接. 然后,得对相应代码做一定调整. 1. 覆盖phpmailer 请从附件进行下载: http://files.cnblogs. ...
- 并发式IO的解决方案:多路非阻塞式IO、多路复用、异步IO
在Linux应用编程中的并发式IO的三种解决方案是: (1) 多路非阻塞式IO (2) 多路复用 (3) 异步IO 以下代码将以操作鼠标和键盘为实例来演示. 1. 多路非阻塞式IO 多路非阻塞式IO访 ...
- 泛函编程(16)-泛函状态-Functional State
初接触泛函状态觉着很不习惯.主要是在使用State数据类型时很难理解其中的原理,特别是泛函状态变迁机制(state transition mechanism):怎么状态就起了变化,实在难以跟踪.我想这 ...
- C#在图片上添加文字代码
创建.NET WinForm程序,设置项目的默认命名空间为Keleyi.Com,在窗体上添加一个PictureBox控件pictureBox_keleyi_com和一个Button控件button_A ...
- Mysql之执行计划
1.explain分析sql语句 例如: EXPLAIN ) ORDER BY bi.`publish_time` 返回结果: 而今天检查的不是这条sql,远比这条复杂,不过也能反映情况了. (1 ...