Maximum Subsequence Sum

  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

题目解析

  本题第一行给出数组长度k,下一行给出k个整数,要求计算出最大连续子串和,并输出最大连续字串的首位与末位,如果整个字符串全为负数,则输出0并输出整个字符串的首位与末位。

  用数组arr记录给出的数组,最大连续子串和,果断dp。用数组dp[i]记录当字符串以第i位结尾时的最大连续子串和。当i = 0时以首位为结尾的最大连续子串和为首位arr[ i ]的值。

之后的值只会有两种情况:

  1)以当前位i为结尾时最大连续子串只有其本身arr[ i ]一个元素;

  2)以当前位i为结尾时最大连续子串有多个元素,即dp[ i - 1] + arr[ i ];

  可以得到状态转移方程dp[ i ] = max(arr[ i ], dp[ i - 1] + arr[ i ]);

  至于最大连续子串的首尾两位我们可以在计算dp数组时一并计算,以bg[ i ]记录dp[ i ]对应的最大连续子串的首位,以ed[ i ]记录dp[ i ]对应的最大连续子串的末尾。

注意:

若不对整个字符串全为负数的情况做出处理, 测试点4会错误;

  若对整个字符串全为负数的情况做出处理了,但将字符串存在0的情况也视为全为负数处理,测试点5会错误。

 #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e4+;
int arr[MAX];
int dp[MAX], bg[MAX], ed[MAX];
int main()
{
int k;
scanf("%d", &k); //输入数组长度k
bool flag = false; //flag记录数组是否全为负数
for(int i = ; i < k; i++){ //输入数组
scanf("%d", &arr[i]);
if(arr[i] >= ) //只要存在正数或0则将flag标记位true
flag = true;
}
dp[] = arr[]; //以首位为结尾的最大连续连续子串和为首位arr[i]的值
int maxSum = arr[], index = ;
//maxSum记录最大连续子串和
//index记录最大连续子串的末位下标
for(int i = ; i < k; i++){
if(dp[i - ] + arr[i] >= arr[i]){ //情况2
dp[i] = dp[i - ] + arr[i];
bg[i] = bg[i - ]; //记录当前最大连续子串首位下标
ed[i] = i; //记录最大连续子串末位下标
}else{ //情况1
dp[i] = arr[i];
bg[i] = ed[i] = i;
//最大连续子串首尾下标都为i
}
if(dp[i] > maxSum){
maxSum = dp[i];
index = i;
}
}
if(flag)
printf("%d %d %d\n", maxSum, arr[bg[index]], arr[ed[index]]);
else
printf("0 %d %d\n", arr[], arr[k - ]);
return ;
}

  当然本题数据量最大只有1e4,暴力的O(n^2)并不会超时,所以本题暴力依然没有问题。

暴力过一切:

 #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e4+;
int arr[MAX], sum[MAX];
int main(){
int k;
scanf("%d", &k);
int temp = ;
for(int i = ; i < k; i++){
scanf("%d", &arr[i]);
temp += arr[i];
sum[i] = temp;
}
int maxSum = -, bg = , ed = k - ;
for(int i = ; i < k; i++){
for(int j = i; j < k; j++){
if(sum[j] - sum[i] + arr[i] > maxSum){
maxSum = sum[j] - sum[i] + arr[i];
bg = i;
ed = j;
}
}
}
if(maxSum < )
maxSum = ;
printf("%d %d %d\n", maxSum, arr[bg], arr[ed]);
return ;
}

PTA (Advanced Level) 1007 Maximum Subsequence Sum的更多相关文章

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

  2. PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

    简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are sup ...

  3. PAT Advanced 1007 Maximum Subsequence Sum

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

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

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

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

  7. 1007 Maximum Subsequence Sum (PAT(Advance))

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

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

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

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

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

随机推荐

  1. java基础-day28

    第05天 MySQL 今日内容介绍 u 数据库的概述 u MySQL和SQLyog的安装和使用 u SQL语句 第1章   数据库的概述 1.1  数据库的概述 1.1.1 概述 l  什么是数据库 ...

  2. Android多线程操作sqlite(Sqlite解决database locked问题)

    参考http://blog.csdn.net/sdsxleon/article/details/18259973  很好 https://github.com/2point0/Android-Data ...

  3. 最大流Dinic算法

    嘿嘿嘿,时隔不久又见到了DInic,再次回顾一下吧 不过这次我倒是不想深究,而是想多做一些题,因为这几次比赛下来,算法不是重点,重点是题目如何转化,算法如何应用,这也是比赛为什么让你带着板子的原因吧, ...

  4. jvm虚拟机--堆内存

    reserved 保留区域 堆 所有对象实例都在这里分配内存. 是垃圾收集的主要区域("GC 堆").现代的垃圾收集器基本都是采用分代收集算法,主要思想是针对不同的对象采取不同的垃 ...

  5. 加密算法比较3DES AES RSA ECC MD5 SHA1等

    加 密算法通常分为对称性加密算法和非对称性加密算法,对于对称性加密算法,信息接收双方都需事先知道密匙和加解密算法且其密匙是相同的,之后便是对数据进行 加解密了.非对称算法与之不同,发送双方A,B事先均 ...

  6. 如何获取帮助———— QQ群讨论摘要

    QQ群对话整理(删除一些简单的回应),对一些重要的地方,我做了一些加粗   宝玉 2015/9/21 1:49:05       这次题目还有个问题就是如何读取Excel,我想对于很多同学来说是个困难 ...

  7. MVC图片验证

    1.创建一个验证类,里面有生成验证码的两个方法. namespace YTJWGL_Common { public class ValidatorCodeTools { #region 生成校验码图片 ...

  8. 数据导出之winfrom导出word(二)

    本篇文章介绍了根据word模板导出word文档的方法. 一.获取模板地址 WordDocFileHelper WordTem = new WordDocFileHelper(); string pat ...

  9. UWP Background过渡动画

    首先说两件事: 1.大爆炸我还记着呢,先欠着吧... 2.博客搬家啦,新地址:https://blog.ultrabluefire.cn/ ==========下面是正文========== 前些日子 ...

  10. 【BZOJ3280】 小R的烦恼(费用流,建模)

    有很浓厚的熟悉感?餐巾计划问题? 不就是多了几个医院,相当于快洗部和慢洗部开了分店. 考虑建图: 如果把每一天拆成两个点,一个表示需求,另一个表示拥有的话. 显然就是一个两边的图,考虑如果我现在有人, ...