PAT1007:Maximum Subsequence Sum
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 思路 DP的思想。
这道题最关键点在于:确定最大值子序列的左右索引left和right。那么:
1.从左到右遍历序列时,如果遍历到第i个数时的累加和已经小于0,那么说明从这个数左边开始的所有可能的左半部序列到这个数的和都已经小于0,对于这个数右半部分的序列只减不增,还不如舍弃掉。因此最大和子序列肯定只会在这个数右边。所以暂存下新的左索引templeft = i + 1。
2.当遍历到第i个数时当前最大值tempsum已经大于输出最大值maxsum时,maxsum = tempsum,那么需要更新左右索引left = templeft 和 right = i.
3.对于遍历后maxsum < 0,说明整个序列只可能全是负数,不存在最大正数和的情况,根据题目要求令maxsum = 0,左右索引即为元序列的左右索引。 代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int N;
while(cin >> N)
{
vector<int> nums(N);
int left = ,right = N - ,maxsum = -,tempsum = ,templeft = ;
for(int i = ;i < N;i++)
{
cin >> nums[i];
tempsum += nums[i];
if(tempsum < )
{
tempsum = ;
templeft = i + ;
}
else if (tempsum > maxsum)
{
left = templeft;
maxsum = tempsum;
right = i;
} }
if(maxsum < )
maxsum = ;
cout << maxsum << " " << nums[left] << " " << nums[right] << endl;
}
}
PAT1007:Maximum Subsequence Sum的更多相关文章
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 }. ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 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 ...
随机推荐
- PR 审批界面增加显示项方法
PR 审批界面增加显示项 解决方法 Step 1: 进入审批界面: Step 2: 在上图中,点击左下角'About this Page'查看数据源 点击上图中'Expand ...
- visual svn使用教程
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什 ...
- ITU-T Technical Paper: 测量QoS的基本网络模型
本文翻译自ITU-T的Technical Paper:<How to increase QoS/QoE of IP-based platform(s) to regionally agreed ...
- utl_file包的使用
首先看一下oracle 脚本 /* # $Header: HTMomse12.sql 12.0.4 20121015 Support $ #+============================= ...
- C语言之归并排序
即将两个都升序(或降序)排列的数据序列合并成一个仍按原序排列的序列. 上代码: #include <stdio.h> #include <stdlib.h> #define m ...
- SharePoint 添加BCD菜单
前言:在SharePoint中,我们常见的操作就是添加我们的自定义BCD菜单,下面,简单介绍下添加自定义BCD菜单的操作.主要介绍两种熟悉的方法,一种通过xml方式,另一种是通过js的方式. 环境:S ...
- Mina源码阅读笔记(六)—Mina异步IO的实现IoFuture
IoFuture是和IoSession紧密相连的一个类,在官网上并没有对它的描述,因为它一般不会显示的拿出来用,权当是一个工具类被session所使用.当然在作用上,这个系列可并不简单,我们先看源码的 ...
- 棋盘的完美覆盖问题,c++代码实现
#include "stdafx.h" #include<iostream> #include<iomanip> using namespace std; ...
- DB Query Analyzer 5.02 is distributed, 53 articles concerned have been published
DB Query Analyzer is presented by Master Gen feng, Ma from Chinese Mainland. It has English version ...
- Robot Framework + Pywinauto 框架实现Windows GUI Automation
Robot Framework is a generic test automation framework for acceptance testing and acceptance test-dr ...