Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1. 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 (≤). 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

方法一:
  

分析:sum为要求的最大和,temp为临时最大和,left和right为所求的子序列的下标,index标记left的临时下标~

temp = temp + v[i],当temp比sum大,就更新sum的值、left和right的值;当temp < 0,那么后面不管来什么值,都应该舍弃temp < 0前面的内容,因为负数对于总

和只可能拉低总和,不可能增加总和,还不如舍弃~舍弃后,直接令temp = 0,并且同时更新left的临时值tempindex。

         int K;
cin >> K;
vector<int>v(K);
int l = , r = K - , sum = -, temp = , index = ;//所求的左、右边界,累加和,以及临时的累加和、左边界
for (int i = ; i < K; ++i)
{
cin >> v[i];
temp += v[i];
if (temp < )//如果和小于0,则直接抛弃
{
temp = ;
index = i + ;//选下一个点为新左点
}
else if (temp > sum)//获得更大值
{
sum = temp;
l = index;
r = i;
}
}
if (sum < )
sum = ;
cout << sum << " " << v[l] << " " << v[r] << endl;

方法二:  

从数组的最后向前算:

当n + 1位置的最大累加和为正数时,那么n的最大累加和一定是自己加上n + 1的最大累加和,其最右边界与n + 1的最右边界相同

当n + 1位置的最大累加和为负数时,那么n的最大累加和一定是自己,因为再向后面加也是加一个负数,其最右边界就是自己的位置

         int K;
cin >> K;
vector<int>v(K);
int l = , r = K - , sum = -;//所求的左、右边界,累加和,以及临时的累加和、左边界
for (int i = ; i < K; ++i)
cin >> v[i]; vector<int>max_sum(K), max_sum_index(K);//当前数能获得最大值的到达的最右端
for (int r = K - ; r >= ; --r)//c从最右端开始加,每次得到自己获取最大值的最优边界
{
if (r + < K && max_sum[r + ] > )//加上大的数会使我变大
{
max_sum[r] = max_sum[r + ] + v[r];
max_sum_index[r] = max_sum_index[r + ];//记录,我这边能到达的最右边是哪
}
else//加上负数会使我变小,还不如自己当最大的数
{
max_sum[r] = v[r];
max_sum_index[r] = r;
}
}
for (int t = ; t < K; ++t)
{
if (max_sum[t] > sum)
{
sum = max_sum[t];
l = t;//自己为左边界
r = max_sum_index[t];//记录点为右边界
}
}
if (sum < )//如果最大和小于0,则所有数都小于0,按要求输出整个数组
{
sum = ;
l = ;
r = K - ;
}
cout << sum << " " << v[l] << " " << v[r] << endl;

PAT甲级——A1007 Maximum Subsequence Sum的更多相关文章

  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. PAT 甲级 1007 Maximum Subsequence Sum

    https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 Given a sequence of K  ...

  3. PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...

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

  5. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  6. PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  7. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  8. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

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

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

随机推荐

  1. neo4j采坑记

    1.安装后启动不起来,解决方案: https://stackoverflow.com/questions/38607283/failed-to-start-neo4j-service  2.一直启动不 ...

  2. HIVE文件

    注册表的本地实体文件, 察看位置,以及映射本地文件到注册表中的位置, HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\hivelist 在这里写 ...

  3. 23种常用设计模式的UML类图

    23种常用设计模式的UML类图 本文UML类图参考<Head First 设计模式>(源码)与<设计模式:可复用面向对象软件的基础>(源码)两书中介绍的设计模式与UML图. 整 ...

  4. JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)

    字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...

  5. js拼接HTML onclick传参,,页面转义符

    字符串 1 使用" .比如: ("'+key+'")例: htmlStr = htmlStr + '<span><img src="'+src ...

  6. Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解

    前言 java 8 中引入的两个与日期相关的新类:Period 和 Duration.两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值.他们 ...

  7. Binary XML file line #23: Error inflating class android.widget.TextView

    分析一波,报错23行TextView的问题,但是检查了xml没有发现23行又TextView相关代码,就不应该继续纠结xml了,代码是通过R文件拿到xml资源的,你就应该怀疑是R文件的问题,R文件编译 ...

  8. COGS2356 【HZOI2015】有标号的DAG计数 IV

    题面 题目描述 给定一正整数n,对n个点有标号的有向无环图进行计数. 这里加一个限制:此图必须是弱连通图. 输出答案mod 998244353的结果 输入格式 一个正整数n. 输出格式 一个数,表示答 ...

  9. 配置虚拟机上的RedHat6 Linux系统的网络(选择的是仅主机模式)

    1.启动虚机,网络选择:仅主机模式 2.进入自己的本地网络配置中,修改相关配置 1)修改VMware NetWork Adapter VMnet1 ip为192.168.137.1,子网掩码:255. ...

  10. thinkphp 控制器定义

    控制器和操作 一般来说,ThinkPHP的控制器是一个类,而操作则是控制器类的一个公共方法. 下面就是一个典型的控制器类的定义: <?php namespace Home\Controller; ...