PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏
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
最大子段和,dp做的,对于每个数,加上他之后如果整体变负了,那就另起开头,如果不是,那就加上去,每次取最大。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std; int a[10004];
int b[10004]; int main()
{
int n,mx,l,r,ml,mr;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
l=r=ml=mr=0;
b[0]=a[0];
mx=b[0];
for(int i=1;i<n;i++)
{
if(b[i-1]<0)
{
b[i]=a[i];
l=r=i;
}
else
{
b[i]=b[i-1]+a[i];
r++;
}
if(mx<b[i])
{
ml=l;
mr=r;
mx=b[i];
}
}
if(mx<0)
{
mx=0;
ml=0;
mr=n-1;
}
printf("%d %d %d\n",mx,a[ml],a[mr]);
return 0;
}
PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏的更多相关文章
- 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 ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】
题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...
- PAT甲 1006. Sign In and Sign Out (25) 2016-09-09 22:55 43人阅读 评论(0) 收藏
1006. Sign In and Sign Out (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT甲 1002. A+B for Polynomials (25) 2016-09-09 22:50 64人阅读 评论(0) 收藏
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- PAT 甲 1005. Spell It Right (20) 2016-09-09 22:53 42人阅读 评论(0) 收藏
1005. Spell It Right (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
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 ...
随机推荐
- Java 枚举那点事
目录 最近有需求,想存自定义的枚举值,比如 HOTLINE("Hotline") 我想存 Hotline 于是研究了一下Java的枚举问题 如下数据库的Entity (贫血模型哈) ...
- c#数和二叉树
树(Tree)是 n(n≥0)个相同类型的数据元素的有限集合.树中的数据元素叫结点(Node).n=0 的树称为空树(Empty Tree):对于 n>0 的任意非空树 T 有: (1)有且仅有 ...
- PPT地图 - 动态显示省份扩散效果
找一张中国地图矢量图,这个要找半天,自己画难度有点大.图中每个省份都是单独一张图.从我的百度云盘下载 提取码: 61ha 选中一个省份图块并复制,填充复制图块的颜色,拖拽该图块覆盖住该省份原来位置. ...
- Winform 两个窗体通讯 一个窗体调用另一个窗体的方法
主要用到 委托 和 注册事件. 功能:点击form1的按钮,改变form2的label文本
- redis lua 用来传输日志
2.8 Lua Script Redis2.6内置的Lua Script支持,可以在Redis的Server端一次过运行大量逻辑,就像存储过程一样,避免了海量中间数据在网路上的传输. Lua自称是在S ...
- Web标准:七、横向导航菜单
Web标准:七.横向导航菜单 知识点: 1.横向列表菜单 2.用图片美化的横向导航 3.css Sprites 1)横向列表菜单 可以在第四节课的基础上来实现横向导航菜单,只要给li一个float ...
- 使用UUID方法生成全球唯一标识
需要生成唯一字符串,如生成应用标识等,可以直接用java.util.UUID类实现. UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字, ...
- <cctype>库
http://www.cplusplus.com/reference/cctype/ 函数名称 返回值 isalnum() 如果参数是字母数字,即字母或数字,该函数返回true isalpha() 如 ...
- 【c++】多层次继承类对象的构造函数参数的传递方法
#include <iostream.h> //基类CBase class CBase { int a; public: CBase(int na) { a=na; cout<< ...
- 每天学一点儿HTML5的新标签
sections部分 (http://www.w3.org/TR/html5/sections.html) 标签:article article标签用来表示页面中一段完整的可以自我包含的片段,具有可重 ...