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 ...
随机推荐
- 消灭Bug!18款最佳的问题跟踪管理应用程序
摘要:工欲善其事,必先利其器,对于开发者来说,处理Bug是一件比较头疼的事,那么如何高效地解决Bug,选择一款合适的Bug跟踪处理工具会让你事半功倍. 对于开发者来说,Bug往往是他们最头疼的问题.有 ...
- MySQL数据库 安装图解
下面的是MySQL安装的图解,用的可执行文件:下载地址:http://www.jinhusns.com/Products/Download/?type=xcj相关下载 mysql安装向导启动,按“Ne ...
- 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu
[源码下载] 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu 作者 ...
- ahjesus解决win下U盘无法写入的问题
可能是由于不同品牌的U盘出厂时磁盘分区和格式化方式不同而引起的兼容性问题.解决方案如下 启动cmd.输入diskpart,启动DISKPART工具 在DISKPART窗口中输入以下命令: >li ...
- Play Framework框架 JPA惯用注解
Play Framework框架 JPA常用注解 1.@Entity(name=”EntityName”) 必须 ,name 为可选 , 对应数据库中一的个表 2.@Table(name=”" ...
- WCF Service部署在IIS上
环境vs2010,WCF应用程序.如何将WCF部署在IIS上. 第一步:右键点击项目,选择生成部署包. 第二步:在你项目所在的文件目录下找到Package文件夹,这就是我们的部署包所在的地方.在这个p ...
- 初学Node(四)事件循环
Node中的事件循环 事件循环是Node的核心,正是因为有了事件循环JS才能够在服务端占有一席之地.JS是一种单线程语言,但是它的执行环境是多线程的在加上JS的事件驱动这一特点,使使JS在执行的过程中 ...
- [ html canvas 模仿支付宝刮刮卡效果 ] canvas绘图属性 模仿支付宝刮刮卡效果实例演示
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- 【转】handler.removeCallbacks失效问题
package com.example.demoactivity; import android.app.Activity; import android.os.Bundle; import andr ...
- [leetcode] Number of Islands
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...