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 ...
随机推荐
- poj 2449 k短路+A*算法
http://poj.org/problem?id=2449 K短路的定义: 1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样. 2.每个顶点和每条边都 ...
- centos救援模式实验笔记
1. 首先在BIOS中把启动选项设置成DVD光驱启动或者USB启动也是可以的 2. 从光盘启动之后再出现的选项中选择“Rescue installed system”然后按回车确认,具体图下图: ...
- FastReport调整字体
- Change tab position of PageControl to bottom
Hi, Try: UniPageControl1 -> ClientEvents -> UniEvents : function tabPanel.beforeInit(sender, c ...
- [leed code 179] Largest Number
1 题目 Given a list of non negative integers, arrange them such that they form the largest number. For ...
- Ubuntu下SVN安装和配置
一.SVN安装 1.安装包 1.$ sudo apt-get install subversion 2.创建项目目录 $ sudo mkdir /home/xiaozhe/svn $ cd /home ...
- python--求参赛两队所有可能的比赛组合情况
朋友遇到一个面试题,让我帮忙实现,题目如下: 红队有A1,B1,C1三名队员,蓝队有A2,B2,C2三名队员,每轮比赛各队出一名队员参加,一名队员只能参加一次比赛,假设A1不会和B2打,B1不会和B2 ...
- Python 学习第三部分函数——第一章函数基础
函数是python 为了代码最大程度的重用和最小代码冗余而提供的最基本的程序结构.使用它我们可以将复杂的系统分解为可管理的部件. 函数相关语句 def... 创建一个对象并将其赋值给 ...
- SSO集成方案[随笔]
看这个方案之前,先说明下为什么要加入SSO,以防对大家产生不好的影响.我们产品使用传统winform+db服务+Db存储方式开发,一群老菜帮子开发,以传统的datatble做数据传递,很多年了未有变化 ...
- 三、winForm-DataGridView操作——DataGridView 操作复选框checkbox
一.添加复选框 ArrayList arr = new ArrayList(); public string checkboxName = "选择"; void StandLibW ...