连续子数组的最大和/1007. Maximum Subsequence Sum (25)
题目描述
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<vector>
#include<stdio.h>
using namespace std;
int dp[],s[]={};
int main()
{
int n,tem;
scanf("%d",&n);
vector<int> vv;
bool is = ;
for(int i = ;i < n ;++i)
{
scanf("%d",&tem);
if(tem >= )
is = ;
vv.push_back(tem);
}
if(is)
{
printf("0 %d %d\n",vv[],vv[n-]);
return ;
}
dp[] = vv[];
for(int i = ;i < n ;++i)
{
if(dp[i-] > )
{
dp[i] = dp[i-] + vv[i];
s[i] = s[i-];
}
else
{
dp[i] = vv[i];
s[i] = i;
}
}
int MAX = -;
int end;
for(int i = ;i < n ;++i)
{
if(dp[i] > MAX)
{
MAX = dp[i];
end = i;
}
}
printf("%d %d %d\n",MAX,vv[s[end]],vv[end]);
return ;
}
连续子数组的最大和/1007. Maximum Subsequence Sum (25)的更多相关文章
- PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. 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 ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 1007 Maximum Subsequence Sum (25)(25 point(s))
problem Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is define ...
随机推荐
- webSocket开源框架:SocketRocket 简单的使用
需要用到webSocket,所以搜集了一下使用方法, git下载地址:square/SocketRocket gitHUB 上没有看懂,就要 cocoaPod 导入了 socketRocket 导入这 ...
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 安卓弹出对话框——Alertdialog
在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...
- python 调用mysql存储过程返回结果集
存储过程: delimiter | ),)) begin select * from tb_test where mid = imid and user = iuser; end; | delimit ...
- Wonderful Sentense
1.Sorry if I might sound arrogant or offensive. 2.Any further question? 3.How dare you! 4.Try it if ...
- javascript面向对象的理解(一)
第一次在园子发文: 关于js面向对象的理解: 工厂方式是什么?构造函数是什么?原形链?对象的引用? 1.对象是什么? 在js接触的比较多的就是对象了,比如: var arr = []; arr.num ...
- java使用POI jar包读写xls文件
主要使用poi jar来操作excel文件.代码中用到的数据库表信息见ORACLE之表.使用public ArrayList<Person> getPersonAllRecords()获得 ...
- [转]Servlet 工作原理解析
Web 技术成为当今主流的互联网 Web 应用技术之一,而 Servlet 是 Java Web 技术的核心基础.因而掌握 Servlet 的工作原理是成为一名合格的 Java Web 技术开发人员的 ...
- 常见的Activity Action Intent常量
Intent的中文意思是“意图,目的”的意思,可以理解为不同组件之间通信的“媒介”或者“信使”. 目标组件一般要通过Intent来声明自己的条件,一般通过组件中的<intent-filter&g ...
- FMS服务器在centos下安装
首先当然得先下载安装包了 http://pan.baidu.com/s/1jGL1Nvw #要先安装一下这个包,否则会提收提示错误,缺少libcap yum install compat-libcap ...