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. 唯一id

    package com.debug.kill.server.utils; /** * Created by Administrator on 2019/6/20. */ import org.apac ...

  2. 在linux 或docker中使用 system.drawing.common

    在dockerfile 中添加 FROM microsoft/dotnet:2.1-aspnetcore-runtime RUN apt-get update RUN apt-get install ...

  3. MySql General error:2006

    当启用模块时发生Mysql数据库错误,错误信息见附件,实际是“General error: 2006 MySQL server has gone away......”错误. 解决方法:找到my.in ...

  4. Windows exit

    退出 CMD.EXE 程序(命令解释器)或当前批处理脚本. EXIT [/B] [exitCode] /B          指定要退出当前批处理脚本而不是 CMD.EXE.如果从一个         ...

  5. thinkphp 获取内容

    如果需要获取渲染模板的输出内容而不是直接输出,可以使用fetch方法. fetch方法的用法和display基本一致(只是不需要指定输出编码和输出类型): 大理石平台规格 fetch('模板文件') ...

  6. Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)F3 - Lightsabers (hard)

    题意:n个数字1-m,问取k个组成的set方案数 题解:假设某个数出现k次,那么生成函数为\(1+x+...+x^k\),那么假设第i个数出现ai次,结果就是\(\sum_{i=1}^m(1+x+.. ...

  7. 手工编写JavaWeb项目

    手工编写JavaWeb项目 一.打开Tomcat服务器 二.编写简单的web项目 三.访问项目 并且,tomcat服务器也是可以直接访问.txt的,其实就和其它的web服务器一样,什么都可以访问,和之 ...

  8. 阿里P8架构师谈:数据库分库分表、读写分离的原理实现,使用场景

    本文转载自:阿里P8架构师谈:数据库分库分表.读写分离的原理实现,使用场景 为什么要分库分表和读写分离? 类似淘宝网这样的网站,海量数据的存储和访问成为了系统设计的瓶颈问题,日益增长的业务数据,无疑对 ...

  9. HTML5: 实现调用系统拍照或者选择照片并预览

    ylbtech-HTML5: 实现调用系统拍照或者选择照片并预览 1.返回顶部 1. <!DOCTYPE html> <html> <head> <meta ...

  10. Spring 基于Aspectj切面表达式(6)

    1 package com.proc; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinP ...