PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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的更多相关文章
- 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, ...
- 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 ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- 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 ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 1007 Maximum Subsequence Sum (PAT(Advance))
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
随机推荐
- noip第1课作业
1. 求三个数的乘积和三次方和 [问题描述] 编程实现输入任意三个整数a, b, c,将这三个数的乘积以及三次方和输出: [样例输入] 1 2 3 [样例输出] 6 36 #include &l ...
- 【并查集的另一个思考方向】POJ1456
POJ1456 这个题一看好像就是用贪心做啊,一个结构体,拍一下序,vis数组一遍遍扫荡,最后输出值,没错,贪心的确能做出来,而这类题目也能应用并查集,实现得思想也是贪心 #include <i ...
- java基础知识-逻辑运算符
/*首先申明:逻辑运算符的操作数都是布尔型表达式*/ /* 演示逻辑运算符 & :两个操作数都为真的时候结果为真 | :两个操作数只要有一个为真,结果就为真 && :短路与,左 ...
- Amoeba常见问题
1.1.1 JAVA_HOME不认 jdk安装后测试无问题java –version,但启动amoeba就是报错JAVA_HOME找不到.就修改/amoeba/bin/amoeba文件,在文件最开头直 ...
- ASP.NET Web API 框架研究 Action的选择
如何从HttpController众多方法里如何选择出有效的Action方法?主要分一下几个步骤: 首先,获取候选HttpActionDescriptor列表(ILookup(string,HttpA ...
- I - Dividing Stones
Description There are N stones, which can be divided into some piles arbitrarily. Let the value of e ...
- MySQL表名大小写设置
1 简介 在MySQL中,数据库对应数据目录中的目录.数据库中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎).因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小 ...
- 关于质能等效的两个思想实验 Two Ideological Experiments on Mass-Energy Equivalence
大家知道,物质和能量是等效的,虽然质能方程已暗示了这种等效关系,但并非显而易见.此等效性可以从以下两个思想实验中获知. 实验一:一台电子称上放置一个金属物体,加热它,称的读数将会略微增加.这是因为金属 ...
- ASP .Net C# ---CSV导入导出
CSV和Excel大致相同 复制下来 数据传到方法里面就可以了 /// <summary> /// DataTable数据写入CSV文件 /// </summary> /// ...
- 【文文殿下】P3740 [HAOI2014]贴海报
题解 一开始想到离散化,然后暴力模拟.但是存在一种hack数据: [5,7] [1,5] [7,9] 这样会错误的认为第一个区间被覆盖了(因为两个端点被覆盖).所以我们设置一个玄学调参系数,在一个区间 ...