PAT甲级——A1007 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方法一:
分析: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的更多相关文章
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 Given a sequence of K ...
- PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】
题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...
- 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 Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
随机推荐
- identityserver4 对接钉钉
参考了https://www.cnblogs.com/sheldon-lou/p/10643267.html
- 在普通类中获取Spring管理的bean
1.在项目中添加下面的类: import org.springframework.context.ApplicationContext; import org.springframework.cont ...
- element-ui表格点击一行展开
转载:https://www.cnblogs.com/xiaochongchong/p/8127282.html <template> <el-table :data="t ...
- struts2类型转换2
如何自定义类型转换器 ? 1). 为什么需要自定义的类型转换器 ? 因为 Struts 不能自动完成 字符串 到 引用类型 的 转换. 2). 如何定义类型转换器: I. 开发类型转换器的类: 扩展 ...
- elast数据存放
这几天一直在索引数据,突然发现服务器状态变红色了,去官网看了下 集群状态如果是红色的话表示有数据已经丢失了!这下头大了才索引了7G数据,后面还有10多个G, 我在liunx下看了下磁盘空间 发现运行e ...
- Delphi 第2课
项目文件.dpr单元文件.pas--目标文件.dcu窗体文件.dfu begin 对象名.属性 对象名.方法end; 在delphi 自动提示控制语句结构 键盘输入Ctrl 就可以看到了 代码错误:( ...
- PKUSC加油加油加油!
一句话,把学过的掌握的甚至还未掌握的,都用上吧! 1.题目不要再再再看错了!在纸上记下关键字. 2.记得有预处理这个东西可以降低复杂度! 3.仔细阅读数据范围,取值范围的0要注意! 4.不要每次像开新 ...
- 网页存储倒计时与解决网页cookie保存多个相同key问题
短信倒计时多用网页临时存储,这可以保证网页在关闭状态也可记时. <p class="test_button" id="getcode">获取验证码& ...
- js--判断当前环境是否为app环境
/** *判断是否是app环境 */ function getIsApp () { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/ ...
- JS switch 分支语句
描述:根据一个变量的不同取值,来执行不同的代码. 语法结构: switch(变量) { case 值1: 代码1; break; case 值2: 代码2; break; case 值3: 代码3; ...