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. [Mysql 查询语句]——分组查询group by

    group by (1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from ...

  2. oracle:ORA-00911: 无效字符 问题和解决---Eclipse中的SQL语句不能加分号

    eclipse中原sql: 异常: 原因:Eclipse中的SQL语句不能加分号 去掉分号,正常执行,插入成功. 这里把id设为了主键,具有唯一性,重复插入同一id执行插入失败,ORA-00001号错 ...

  3. [javaSE] IO流(装饰设计模式)

    装饰设计模式:当想要对已有的对象进行功能增强时,可以自定义类将已有的对象传入,并提供加强功能,自定义的该类称为装饰类 典型的: Reader--FileReader --BufferedReader ...

  4. Java学习--Calendar 类的应用

    Calendar 类的应用 Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calen ...

  5. win下gosublime配置ctag

    ctags(Generate tag files for source code)是vim下方便代码阅读的工具.尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM.并且VIM中已经默认安装 ...

  6. python循环删除列表里的元素!漏删!

    li = [1,2,3,4,5,6] for i in li: if i<3: li.remove(i) print(li) #输出的结果是 [2,3,4,5,6]    2没有remove掉 ...

  7. PHP之httpRequest

    <?php /** * Respose A Http Request * * @param string $url * @param array $post * @param string $m ...

  8. Win10安装docker步骤

    最近使用docker部署spring boot项目,写篇文章记录下步骤. 1. 确保本机win10系统虚拟化已启动启动,否则需要去BIOS设置(方法可百度) 2. 到docker网站下载DockerT ...

  9. 基于openlayers2军事标绘,开源

    1.其实各GIS公司.网络上 都会有提供 军事标绘的功能,如arcgis 的,超图的.mapgis的:但大多数是非开源.且收费的.2.在这里要感谢超图,超图开源了标绘扩展符号库,我这里使用的就是超图的 ...

  10. C++学习笔记(7)----类的数组中构造函数和析构函数的调用顺序

    C++类的数组中构造函数和析构函数的调用顺序(2) 对于如下的代码: #include<iostream> using namespace std; class CBase { priva ...