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. eclipse中ctrl+K失效

    从昨天开始eclipse中Ctrl+d和Ctrl+K就失效了,简直无法忍受 解决方案: Window --> preferences --> General --> keys

  2. C/C++ 打开串口和关闭串口

    通常使用下列函数来通过Win系统来对外围设备进行通信处理: 0. 前言 做串口方面的程序,使用CreateFile打开串口通信端口.在对串口操作之前,需要首先打开串口.使用C++进行串口编程,如果采用 ...

  3. day03_雷神_文件操作

    day03 上周回顾_问题总结: 地址值: li = [] name = ['name','price','count'] dic = {} #如果这里定义空列表,后边的dic[name[i]] = ...

  4. Delphi TStringHelper用法详解

    Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...

  5. WINDOWS CLUSTER -- 时间不同步导致的群集问题

    故障描述,重启服务器后,发现该重启节点未成功加入到Windows群集中,导致该节点上的Alwayson服务也受影响处于“正在解析”状态,尝试重启cluster服务,发现无效,查看windows日志,发 ...

  6. 【VB.NET】通过 IPIP.NET 数据库来查询IP地址

    上一次介绍了利用纯真数据库查询IP地址详细信息的方法.然而纯真数据库是由网友反馈所提供的,很多数据描述并不准确,所以我上网找了一些其他的IP数据库,最后就找到了 ipip.net 这个网站所提供的IP ...

  7. 如何在C#中引入CPLEX的dll(CPLEX系列-教程一)

    以前写在CSDN上的文章.转到博客园之后,打算把这个教程移过来,顺便完善后面的教程.主要是在Asp.Net+EF6里面使用cplex,完成一个最优生产计划的决策.当时在查找如何在C#中引用cplex时 ...

  8. js中两种for循环的使用

    针对两种for循环的使用 1. for in循环的使用环境     可用在字符串.数组.对象中, 需注意:其中遍历对象得到的是每个key  的value值  2. for 变量递加的方式        ...

  9. 14_python 匿名函数,递归函数

    一.匿名函数 语法: 函数名 = lambda 参数: 返回值    # lambda x,y,z=1:x+y+z 注意: 1.函数的参数可以有多个. 多个参数之间⽤逗号隔开  2.匿名函数不管多复杂 ...

  10. yum 安装mysql数据库

    1.先查看是否有安装mysql,有的话通过yum remove mysql先卸载掉,卸载完成后执行 yum install -y mysql-server mysql mysql-deve 2.启动m ...