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 ...
随机推荐
- WPF使用Path画圆的一个示例代码
<Path Fill="red" Data="M 0,0 A 20,20 45 1 1 0,1 Z"/> 1. Fill是指Path的填充颜色 2. ...
- concurrency基础
Runnable 一个执行任务,没有返回值,也不能抛出受检查异常 Callable 一个执行任务有返回值,也能抛出受检查异常 Future 表示执行任务的生命周期,任务的生命周期为:创建,提交,开始, ...
- [笔记] Python基础---列表
1.定义列表 列表由数据构成的有限序列,即按照一定的线性顺序排列,排列而成的数据项的集合. 2.创建列表 使用‘[ ]’括起来就已经创建了一个列表,例: my_first_list = [] #空列表 ...
- Object的原型拷贝-create、assign、getPrototypeOf 方法的结合
一.实现原型拷贝 1.1.代码 tips:为了体现原型链,写了继承实现的代码,这部分可跳过- <script> /* 创建包含原型链的实验对象obj1-- start */ ...
- CSS3动画积累+动画库+3d动画
一.animates.css animate.css是来自dropbox的工程师Daniel Eden开发的一款CSS3的动画效果小类库.包含了60多款不同类型的CSS3动画,包括:晃动,闪动,各种淡 ...
- jsonp全国天气案例
案例1: 1.获取跨域数据 2.将数据按照下面的效果放到body里面 key: f49570d39b02b3c203526b5d8255aa61 079179afb105ce2bae9f5d0 ...
- Simotion 监控问题:Could not add self-signed certificate to certificate store.
使用OPC UA 连接设备,在创建客户端证书时总是报这个错误:Could not add self-signed certificate to certificate store.. 解决方法,用管理 ...
- SQL Serever学习7——数据表2
修改数据表的属性 保存数据表的修改的时候,总会弹出阻止修改的警告. 解决方法2种: 1.可以使用T-sql语句强行修改 2.工具>选项>设计器>表设计器和数据库设计器>阻止保存 ...
- 面向对象(基础oop)之属性与构造函数
大家好,我叫李京阳,,很高兴认识大家,之所以我想开一个自己的博客,就是来把自己所了解的知识点通过自己的话写一下,希望被博客园的朋友们点评和一起讨论一下,也希望从博客园中多认识一些软件开发人员!现在我开 ...
- 述一个程序员的技能:系统安装(win7版)idea配置
idea配置:http://www.phperz.com/article/15/0923/159043.html 作为一名计算机专业出身的程序员,组装电脑和安装系统是基本技能.打造一个安全稳定高效的开 ...