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 ...
随机推荐
- SQL中intersect、union、minus和except 运算符
1.intersect运算符intersect运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表.当 ALL 随 INTERSECT 一起使用时 (inte ...
- 补习知识:Entity Framework Code First属性映射约定
Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...
- Xml+Xslt测试工具
下载地址:http://download.csdn.net/detail/a497785609/5791359 说明:下载后,修改下Xslt部分的头部:<xsl:stylesheet versi ...
- app store上传图片显示错误:未能创建 屏幕快照
在iTunes Connect中加入一个app后.加入屏幕快照时,依照要求的尺寸上传照片成功,可是在保存的时候提示"未能创建Screenshots for 4-inch iPhone5 an ...
- unity调用系统剪切板功能
package com.game.utils; import android.app.Activity; import android.content.ClipData; import android ...
- ListView局部更新(非notifyDataSetChanged)
package com.example.test; import java.util.ArrayList; import java.util.List; import android.app.Acti ...
- The Way to Go读书笔记_第4章_基本结构和基本数据类型
“_”标识符 _ 本身就是一个特殊的标识符,被称为空白标识符.它可以像其他标识符那样用于变量的声明或赋值(任何类型都可以赋值给它),但任何赋给这个标识符的值都将被抛弃,因此这些值不能在后续的代码中使用 ...
- 从Python的角度来看编码与解码
导语: Python2和Python3中,因为默认字符集的不同而造成的麻烦,简直是程序员的梦魇!要彻底告别这个麻烦,就需要从本质上来理解编码和解码. 为什么要有编码? 对于不会英文的中国人来说,将英文 ...
- PyCharm Python迁移项目
把整个项目文件迁移过去后,执行文件会报不能执行XX,系统找不到指定的文件. 此时把当前的这个文件名字改一下,再运行,修改提示的错误.等错误全部修改,可以正常运行后,再把文件名改回去
- 我的vimrc文件
vim的一直被称为神器,确实有很多优点,但是vim到一键编译实在是一个大问题,网络上有很多配置文件,但是大多都是同一份文件到复制粘贴,不太好用. 经过这么长时间到摸索,我终于在自己到电脑上配置好了vi ...