1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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(最长子段和)的更多相关文章

  1. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

  2. PAT 1007 Maximum Subsequence Sum 最大连续子序列和

    Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni ...

  3. PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)

    Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...

  4. PAT 1007 Maximum Subsequence Sum (25分)

    题目 Given a sequence of K integers { N​1​​ , N​2​​ , ..., N​K​​ }. A continuous subsequence is define ...

  5. [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]选择的是 ...

  6. 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 ...

  7. 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 ...

  8. 1007 Maximum Subsequence Sum (PAT(Advance))

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  9. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

随机推荐

  1. html5-canvas绘图操作方法

    <script>function draw(){    var c=document.getElementById("mycanvas");    c.width=50 ...

  2. css - 当文本内容长度超出屏幕宽度时,以省略号代替

    <style> .ellipsis{ text-overflow: ellipsis; overflow: hidden; white-space: nowrap; } </styl ...

  3. 点滴积累【C#】---TreeView读取数据库

    效果: 数据库: 思路: 利用for遍历,然后创建父节点,再根据父节点创建出子节点. 代码: using System; using System.Collections.Generic; using ...

  4. Atitit.atiJsBridge 新特性v7q329

    Atitit.atiJsBridge 新特性v7q329 atiJsBridge 未来计划 Postdata  图像上传的支持 Simp param计划 p1 p2 p3 p4 $method 的si ...

  5. hash slot(虚拟桶)

    在分布式集群中,如何保证相同请求落到相同的机器上,并且后面的集群机器可以尽可能的均分请求,并且当扩容或down机的情况下能对原有集群影响最小. round robin算法:是把数据mod后直接映射到真 ...

  6. CSU 1335: 高桥和低桥 (二分查找,树状数组)

    Description 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不 ...

  7. UITableviewcell的性能问题

    iOS开发UI篇—UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...

  8. Redis-ha(sentinel)

    redis的sendtinel 是用来管理多个redis服务器的 作用 • 监控:监控主从服务器是否运作正常(通过给服务器发送心跳包的方式)    • 提醒:当某个Redis服务器出现异常时,可以通过 ...

  9. linux学习笔记9--命令cat

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. cat命令连接文件并打印到标准输出设备上, ...

  10. android studio 中配置androidAnnotation 的新版正确配置

    apply ].processResources.manifestFile resourcePackageName 'com.peiandsky.firstandroidstudio' }}