PAT 1007 Maximum Subsequence Sum(最长子段和)
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 <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#include <strstream>
#include <string> using namespace std;
#define MAX 10000
int dp[MAX+5];
int a;
int n;
int main()
{
int max;
int begin;
int end;
int sum;
int x;
scanf("%d",&n);
scanf("%d",&a);
sum=a;
max=a;
begin=end=a;
int s=a;
x=a;
for(int i=2;i<=n;i++)
{
scanf("%d",&a);
if(sum>=0)
sum+=a;
else
{
sum=a;
x=a;
}
if(max<sum)
{
max=sum;
begin=x;
end=a;
}
}
if(max<0)
cout<<0<<" "<<s<<" "<<a<<endl;
else
cout<<max<<" "<<begin<<" "<<end<<endl;
return 0;
}
PAT 1007 Maximum Subsequence Sum(最长子段和)的更多相关文章
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- PAT 1007 Maximum Subsequence Sum 最大连续子序列和
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni ...
- PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)
Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...
- 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
经典最大连续子序列,dp[0]=a[0],状态转移dp[i]=max(dp[i-1]+a[i],a[i])找到最大的dp[i]. 难点在于记录起点,这里同样利用动态规划s[i],如果dp[i]选择的是 ...
- 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 ...
- 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 (PAT(Advance))
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
随机推荐
- Actors编程模型
Actors模型(Actor model)首先是由Carl Hewitt在1973定义, 由Erlang OTP (Open Telecom Platform) 推广,其 消息传递更加符合面向对象的原 ...
- NoSQL(三)
redis集群介绍 1.官方叫cluster,redis3.0才直接的一个架构,如果数据量很大,单台机器已经无法满足存储,查询的瓶颈,所以我们需要多台机器构成一个大集群,用来解决存储空间,查询速度,负 ...
- 重置linux mysql root密码
2.修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/my ...
- unity, access sprite of UGUI Image
首先需要using UnityEngine.UI; 然后调用下面语句就不报错了: Image.GetComponent<Image>().sprite 参考:http://answers. ...
- 使用ng-grid实现可配置的表格
使用Angularjs在带来方便的同时,也有一些遗憾:很多基于jquery或其它的组件,在angularjs中需要集成一下才能用得流畅.但是一些比较复杂的组件,集成起来的工作量相当大,比如说grid. ...
- datatables 相关文章
http://blog.csdn.net/zhu_xiao_yuan/article/details/51252300 datatables参数配置详解 http://blog.csdn.net/j ...
- ubuntu16.0.4 update git
Ubuntu 16.04 comes with Git 2.7.x, which is a little old now. As versions 2.8 & 2.9 are not part ...
- 用Vue.js递归组件构建一个可折叠的树形菜单
在Vue.js中一个递归组件调用的是其本身,如: Vue.component('recursive-component', { template: `<!--Invoking myself! ...
- 用node.js进行微信公众平台的开发
基本原理 用nodejs怎样来实现对微信公众平台的开发呢? 别的就不多说了,先来简单介绍微信公众平台的基本原理. 微信服务器就相当于一个转发服务器,终端(手机.Pad等)发起请求至微信服务器,微信服务 ...
- CRF++使用说明
摘选自 :CRF++: Yet Another CRF toolkit Usage: 1.输入输出文件 He PRP B-NP reckons VBZ B-VP the DT B-NP current ...