1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  2. pat1007. Maximum Subsequence Sum (25)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

  4. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  5. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  6. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  7. 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 ...

  8. 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 ...

  9. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

随机推荐

  1. 【一天一道LeetCode】#38. Count and Say

    一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...

  2. 如何优化你的布局层级结构之RelativeLayout和LinearLayout及FrameLayout性能分析

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/51159419 如何优化你的布局层级结构之RelativeLayout和LinearLa ...

  3. ros_indigo使用keyboard键盘控制虚拟或实际机器人

    首先,上参考网址: http://wiki.ros.org/Robots/TIAGo http://wiki.ros.org/teleop_twist_keyboard 第一个,使用TIAGo的gaz ...

  4. 图像检索:CEDD(Color and Edge Directivity Descriptor)算法

    颜色和边缘的方向性描述符(Color and Edge Directivity Descriptor,CEDD) 本文节选自论文<Android手机上图像分类技术的研究>. CEDD具有抽 ...

  5. 在 ServiceModel 客户端配置部分中,找不到引用协定“XX”的默认终结点元素的解决方法

    今天在CRM2011中写Plugin的的时调用WCF报错,错误如下:"在 ServiceModel 客户端配置部分中,找不到引用协定"XX"的默认终结点元素.这可能是因为 ...

  6. Graph Cut and Its Application in Computer Vision

    Graph Cut and Its Application in Computer Vision 原文出处: http://lincccc.blogspot.tw/2011/04/graph-cut- ...

  7. 【面试笔试算法】Program 6: 字符消除(hiho题库)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1) ...

  8. OpenGL Shader Key Points (2)

    1.  Uniform 1.1.  Uniform变量 不是所有的变量都是跟顶点一一对应的,如变换矩阵,光源位置等. Uniform变量可以在任何类型的shader中使用,但只能作为输入值,不能在sh ...

  9. 运行Myeclipse时,如何删除IVM窗口

    windows------>preference------>run/debug------->lauching--------->percpectives,改成never,n ...

  10. ListView 与ContextMenu的关联管理

    <span style="font-family: Arial, Helvetica, sans-serif;">package com.example.listvie ...