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 ...
随机推荐
- C#的可空类型与不可空类型
null:为空,表示产量没有指向任何对象, 如:string = null; int a = null;//是错的 但可以这样,在类型后面加一个问号. int? a = null;//这就是正确的,是 ...
- Comet技术在Java Web中的应用
HTTP协议是互联网上大量信息交换的基础,其特点是,它是基于请求-响应模式的无状态的单向协议,即,必须由客户端发起一个请求建立连接,服务器接收请 求,把数据返回给客户端,然后释放连接.下一次,再由客户 ...
- java 继承多态的一些理解和不理解
1.向上转型的一个误区 一直以为Child 继承Parent以后, Parent p = new Child(); p可以调用Child类中拓展Parent的方法,原来必须在强制转换成Child类才 ...
- 把AspDotNetCoreMvc程序运行在Docker上-part2:修改容器以及发布镜像
在上一个part<把AspDotNetCoreMvc程序运行在Docker上-part1>,已经将成功将aspdotnetcore程序运行在两个不同的容器中,目前两个容器的内容完全相同,只 ...
- Thread中断线程的方法
转载:https://www.cnblogs.com/l2rf/p/5566895.html 线程对象属于一次性消耗品,一般线程执行完run方法之后,线程就正常结束了,线程结束之后就报废了,不能再次s ...
- DQ8通关攻略
<勇者斗恶龙8>作为勇者斗恶龙系列首次实现3D的一作,游戏无论是从画面.音效还是游戏系统都表现非常不俗,这款游戏也是PS2主机上必玩的一款大作. 作为PS2平台上唯一一款勇者斗恶龙的正传新 ...
- NFS挂载时出现"access denied by server while mounting"的解决方法
NFS挂载时出现"access denied by server while mounting"的解决方法 2015-01-14 何敏杰 3条评论 44,071次浏览 NFS是 ...
- JAVA工具系列之——Postman
1 概述 Postman是一款测试rest接口的工具,可以实现前端未实施的情况下,后端同步开发.本文从部署到运用进行展开描写. 2 部署 第一步:进入Postman官网下载最新版本,下载链接 第二步: ...
- Mybatis之简单注解
Mybatis使用注解实现主键自增长: oracle: @SelectKey(statement="select my_seq.nextval from dual",resultT ...
- Jquery把获取到的input值转换成json
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...