题目描述

HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?
 
 

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
 #include<vector>
#include<stdio.h>
using namespace std;
int dp[],s[]={};
int main()
{
int n,tem;
scanf("%d",&n);
vector<int> vv;
bool is = ;
for(int i = ;i < n ;++i)
{
scanf("%d",&tem);
if(tem >= )
is = ;
vv.push_back(tem);
}
if(is)
{
printf("0 %d %d\n",vv[],vv[n-]);
return ;
}
dp[] = vv[];
for(int i = ;i < n ;++i)
{
if(dp[i-] > )
{
dp[i] = dp[i-] + vv[i];
s[i] = s[i-];
}
else
{
dp[i] = vv[i];
s[i] = i;
}
}
int MAX = -;
int end;
for(int i = ;i < n ;++i)
{
if(dp[i] > MAX)
{
MAX = dp[i];
end = i;
}
}
printf("%d %d %d\n",MAX,vv[s[end]],vv[end]);
return ;
}

连续子数组的最大和/1007. Maximum Subsequence Sum (25)的更多相关文章

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

  2. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

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

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

  4. 1007 Maximum Subsequence Sum (25 分)

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

  5. PAT 1007 Maximum Subsequence Sum (25分)

    题目 Given a sequence of K integers { N​1​​ , N​2​​ , ..., N​K​​ }. A continuous subsequence is define ...

  6. PAT 解题报告 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

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

  8. 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

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

随机推荐

  1. onInterceptTouchEvent和onTouchEvent举例分析

    首先自定义三个组件,其关系是:MyLayout在最上面,MySubLayout在MyLayout下面,MyView在MySubLayout下面. 一个点击事件进来,首先是DOWN动作,先是MyLayo ...

  2. a 中调用js的几种方法

    我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题 ...

  3. 21世纪C语言(影印版)

    <21世纪C语言(影印版)> 基本信息 原书名:21st Century C 作者: Ben Klemens 出版社:东南大学出版社 ISBN:9787564142056 上架时间:201 ...

  4. JSP起源、JSP的运行原理、JSP的执行过程

    JSP起源 在很多动态网页中,绝大部分内容都是固定不变的,只有局部内容需要动态产生和改变. 如果使用Servlet程序来输出只有局部内容需要动态改变的网页,其中所有的静态内容也需要程序员用Java程序 ...

  5. xe5 android listbox的 TMetropolisUIListBoxItem

    listbox实现以下效果: 关键代码,采用数据集的方式 type PpatientData=^RpatientData; RpatientData= record patient_id:string ...

  6. hdu 4412 利用单调性的动态规划

    思路: 这题和1227的求法一样,只不过1227是小数据,暴力下,就能进行预处理. 这题的预处理区间期望cost[i][j]需要利用单调性. 即假使以pos位置为安排的点,那么这个区间在其左边的概率为 ...

  7. codevs4600 [NOI2015]程序自动分析==洛谷P1955 程序自动分析

    4600 [NOI2015]程序自动分析  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 在实现 ...

  8. sql导出查询数据

    from sysobjects where name ='proc_exporttb' and xtype ='P') drop proc proc_exporttb go create proc p ...

  9. 如何在MVC中显示条形码图片(以内存流的方式)

    前台代码: <script type="text/javascript"> function fresh() { var getimagecode = document ...

  10. sql语句使用游标修改表中数据

    declare @a varchar(),@b varchar() declare user_cursor cursor for select a,b from tableA tab open use ...