PTA | 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≤i≤j≤K. 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 (≤10000). 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
作者: 陈越
#include <stdio.h> void MaxSubseqSum(int A[], int N)
{
int ThisSum = , MaxSum = ;
int beg, end;
int tmp = A[];
int max = A[];
for (int i = ; i < N; i++)
{
ThisSum += A[i];
if (A[i] > max)
{
max = A[i];
}
if (ThisSum > MaxSum)
{
MaxSum = ThisSum;
beg = tmp;
end = A[i];
}
else if (ThisSum < )
{
ThisSum = ;
tmp = A[i + ];
}
}
if (max < )
{
beg = A[];
end = A[N - ];
}
else if (max == )
{
beg = end = ;
}
printf("%d %d %d", MaxSum, beg, end);
} int main()
{
int N;
int num[];
scanf("%d", &N);
for (int i = ; i < N; ++i)
{
scanf("%d", &num[i]);
}
MaxSubseqSum(num, N); return ;
}
提交结果
| 测试点 | 提示 | 结果 | 耗时 | 内存 |
|---|---|---|---|---|
| 0 | sample换1个数字。有正负,负数开头结尾,有并列最大和 | 答案正确 | 2 ms | 384KB |
| 1 | 最大和序列中有负数 | 答案正确 | 1 ms | 384KB |
| 2 | 并列和对应相同i但是不同j,即尾是0 | 答案正确 | 2 ms | 256KB |
| 3 | 1个正数 | 答案正确 | 2 ms | 384KB |
| 4 | 全是负数 | 答案正确 | 2 ms | 256KB |
| 5 | 负数和0 | 答案正确 | 2 ms | 256KB |
| 6 | 最大和前面有一段是0 | 答案正确 | 2 ms | 256KB |
| 7 | 最大N | 答案正确 | 3 ms | 384KB |
PTA | Maximum Subsequence Sum的更多相关文章
- PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum (25分) Given ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 01-复杂度2 Maximum Subsequence Sum
01-复杂度2 Maximum Subsequence Sum (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
随机推荐
- Integer 与 int -- 自动装箱(autoboxing)与自动拆箱(unboxing)
转载于 http://www.ticmy.com/?p=110 jdk1.5引入了自动装箱(autoboxing)与自动拆箱(unboxing),这方便了集合类以及一些方法的调用,同时也使初学者对其感 ...
- MySQL数据库-错误1166 - Incorrect column name 'xxx' 的解决方法
在用Navicat for MySQL给MySQL数据库修改表的字段时报如下的错误: 解决方法:检查字段里面是不是有空格,去掉就可以了.
- CSS- 文本超出指定宽度后隐藏并显示为省略号
一般的文字截断(适用于内联与块): .text-overflow { display:block;/*内联对象需加*/ width:25em; word-break:keep-all;/* 不换行 * ...
- pygame(一)
昨天,是有在树莓派上面操作pygame的,但是树莓派又上不了网了,很奇怪,我的306wifi显示的是树莓派连接成功,但是就是无法用网络,所以就下载不了图片,坐等HDMI线吧. 现在,看小甲鱼的视频已经 ...
- C#中使用Log4Net记录日志
https://www.cnblogs.com/W--Jing/p/8125652.html 实例参考 https://www.cnblogs.com/soundcode/p/4866078.html ...
- IE8实现媒体查询
IE8实现媒体查询 实现IE8引用样式 <!--[if IE 8]> 仅IE8可识别 <style> @import "Index.css"; } < ...
- 六、curator recipes之屏障barrier
简介 curator针对分布式场景实现了分布式屏障:barrier.我们在分布式系统中可以使用barrier去阻塞进程,知道某个条件被触发.其实跟Java多线程的barrier是一样的. 例如:当两个 ...
- HDFS 的运行机制
hdfs haddop distributed system 由 name node, secondary name node,data node, client 组成. 真正存放数据的就是 data ...
- jvm 类文件结构学习
本文以代码示例来学习 java 类文件的结构,其中对类文件结构的学习均来自周志明先生所著的 <深入理解 Java 虚拟机>一书,在此表示诚挚的感谢. 代码如下: package com.r ...
- 时间复杂度为O(logN)的常用算法
时间复杂度为O(logN)的常用算法 折半查找 /* * 折半查找 * 默认查找的数组已经排过序 */ public static int binarySearch(int[] a,int x){ i ...