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
最大子段和,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) 收藏的更多相关文章

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

  2. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  3. PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...

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

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

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

  7. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

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

  8. 1007 Maximum Subsequence Sum (25 分)

    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. Session的作用和使用场景

    1.session何时被创建? 客户首次访问服务器时,回话session对象被创建并分配一个唯一的Id,同时id号发送到客户端,并存入cookie,使得客户端session对象和服务器端一致. 2.如 ...

  2. 201671010127 2016—2017—2 Java怎样解决Java程序中中文乱码的问题。

    这是本次第二次分享新手在编程中遇到的问题,相信很多Java新手和我一样,在Java编程中会遇到中文乱码的情况,下面我就给大家分享我遇到问题和解决问题的具体过程. 我先用Notepad++写了一个如下的 ...

  3. (mac环境)Appium安装了client包,但是提示no module named appium

    背景 mac环境,使用pip install Appium-Python-Client已经安装了client包   问题 import appium,提示no module named appium ...

  4. 第八章 高级搜索树 (xa2)红黑树:结构

  5. Java label

    标号label提供了一种简单的break语句所不能实现的控制循环的方法.当你嵌套在几层循环中想退出循环时,break只能退出一重循环,可以用标号标出想退出哪一个语句. 标号的命名不能以"_& ...

  6. python作业之生成随机数

    作业要求 生成一个6个字符长度的随机数,要求是包括字母和数字的组合 import random l1 = [] for i in range(6): a = random.randrange(0,10 ...

  7. 4-QT的程序打包发布(将QT5的工程项目打包成一个exe程序)

    https://blog.csdn.net/windsnow1/article/details/78004265 最近,在学习QT5的过程中,想尝试着把自己写的工程程序给打包发布出来,在任何一台win ...

  8. Tomcat设置默认时区

    本文讲解如何在tomcat启动时设置JVM默认时区. 环境:JDK1.8.114 web容器:Tomcat 9 tomcat启动脚本 /etc/init.d/tomcat 操作系统ubuntu 16 ...

  9. linux小白

    1. linux下加域名. 文件是在/etc/hosts    中间加的tab键 192.168.0.1 baidu.com linux下测试网页可以用 wget www.baidu.com  这个命 ...

  10. spring mvc , spring data jpa 知识点

    <mvc:view-controller path="/login" view-name="login.jsp"/> <!-- 会直接跳到/W ...