Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } 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
 

作者: 陈越

单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB
代码长度限制: 16 KB

 #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的更多相关文章

  1. 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 ...

  2. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  3. 01-复杂度2 Maximum Subsequence Sum

    01-复杂度2 Maximum Subsequence Sum   (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...

  4. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

  5. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  6. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  7. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  9. 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 ...

随机推荐

  1. SpringCloud - 2. 服务注册 和 发现

    SpringCloud 的服务注册和发现是由Eureka来完成. 1.eureka server 1.1 依赖 <dependency> <groupId>org.spring ...

  2. 十分钟理解Actor模式

    Actor模式是一种并发模型,与另一种模型共享内存完全相反,Actor模型share nothing.所有的线程(或进程)通过消息传递的方式进行合作,这些线程(或进程)称为Actor.共享内存更适合单 ...

  3. C#泛型设计的一个小陷阱.

    距离上次发表博客已经有几年了. 对于没能坚持更新博客,实在是感觉到甚是惭愧. 闲言少叙, 直接切入主题. 背景 最近一直在对于公司一个网络通信服务程序使用.net core 进行重构.重构的目的有两个 ...

  4. ViewPager(视图滑动切换工具)

    <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.Cons ...

  5. java并发编程(9)内存模型

    JAVA内存模型 在多线程这一系列中,不去探究内存模型的底层 一.什么是内存模型,为什么需要它 在现代多核处理器中,每个处理器都有自己的缓存,定期的与主内存进行协调: 想要确保每个处理器在任意时刻知道 ...

  6. c#和java中封装字段的不同

    c#: private String name; public String Name { get { return name; } set { name = value; } } .csharpco ...

  7. Vue 脱坑记 - 查漏补缺(汇总下群里高频询问的xxx及给出不靠谱的解决方案)

    前言 发现群里有些问题的提问重复率太高了,每次都去回答,回答的贼烦.这里做一个大体的汇总,废话不多说,直接开始给出方案,不是手把手..若是连问题和解决都看不懂的..应该去补充下基础知识 问题汇总 Q: ...

  8. 方法执行一次js

    var isFirst = true; $(function () { //一级 $("#City").change(function () { var url = "/ ...

  9. Vue 多路由文件的合并

    Vue 多路由文件的合并 1.使用的是ES6 数组的合并方法 let routes = new Set([...routes1, ...homerouters]);2.两个路由文件,导出的实际上就是一 ...

  10. C# 学习笔记(一) Winform利用Assembly反射动态创建窗体

    1. 添加Reflection //添加对Reflection程序集引用 using System.Reflection; // 引用窗体创建方法CreateForm,传入参数 private voi ...