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. 【转】详解tomcat的连接数与线程池

    对tomcat线程池.Connector的BIO.NIO解析的很透彻的一篇文章. 原文链接:https://www.cnblogs.com/kismetv/p/7806063.html 前言 在使用t ...

  2. css 内容溢出显示垂直滚动条,内容不超出就不显示滚动条

    搬运自:https://www.cnblogs.com/wangyuanyuanlovexuanxuan/p/7767767.html html: <style> .div1{ width ...

  3. Activiti学习笔记11 — 判断节点的使用

    一. 创建流程 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns=" ...

  4. JS对象 JavaScript 中的所有事物都是对象,如:字符串、数值、数组、函数等,每个对象带有属性和方法。

    什么是对象 JavaScript 中的所有事物都是对象,如:字符串.数值.数组.函数等,每个对象带有属性和方法. 对象的属性:反映该对象某些特定的性质的,如:字符串的长度.图像的长宽等: 对象的方法: ...

  5. fso文件夹操作用法实操

    Sub 订单转换()Application.ScreenUpdating = FalseOn Error Resume Next Dim fso, fl, m%, n%, p%, q& Dim ...

  6. SVN Cannot merge into a working copy that has local modifications

    我尝试了 主支,分支都提交,但是依然无法合并. 最终,我在服务器上将分支删除,然后主支在拷贝过去. 一,打开服务器资源 二,删除分支 三,拷贝主支到分支 四,刷新分支,就能看到了. 然后在分支项目中, ...

  7. java哈希表(线性探测哈希表。链式哈希表)

    哈希表(散列表) 通过哈希函数使元素的存储位置与它 的关键码之间能够建立一一映射的关系,在查找时可以很快找到该元素. 哈希表hash table(key,value) 的做法其实很简单,就是把Key通 ...

  8. P1820 寻找AP数

    P1820 寻找AP数两个性质,分解质因数后,连续,且指数递减,dfs就完了 #include <iostream> #include <cstdio> #include &l ...

  9. 解决方案 -SQL脚本建表产生ORA-00942错误

    一.问题简介 1.开发环境 操作系统:win10 数 据 库:Oracle11g 数据库连接工具:Navicat  Premium 2.问题简述 在使用SQL Development.Navicat  ...

  10. re.groups取出来的空元祖??

    源自学习笔记: day23_1_re_ groups方法取出来的字符是空的元组??为啥? ''' # ------------------------------------------------- ...