Given a sequence of K integers { N1, N2, ..., N**K }. A continuous subsequence is defined to be { N**i, N**i+1, ..., N**j } where 1≤ijK. 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

思路

最大连续子序列和。这个题会卡一组数据。

input
4
0 0 0 -1 output
0 0 0
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int N;
int v[10000 + 10];
int s1, e1, s, e;
int sum = -1; int main(){
//input
cin >> N;
for(int i = 0; i < N; i++){
cin >> v[i];
}
//init
s1 = v[0];
e1 = v[0];
s = v[0];
e = v[N - 1];
//compute
int b = 0;
for(int i = 0; i < N; i++){
if(b >= 0){
b += v[i];
e1 = v[i];
}
else{
b = v[i];
e1 = v[i];
s1 = v[i];
}
if(sum < b){
s = s1;
e = e1;
sum = b;
}
}
sum = max(sum, 0);
cout << sum << " " << s << " " << e << endl; return 0;
}

PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)的更多相关文章

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

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

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

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

  3. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  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)(25 分)(0不是负数,水题)

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

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

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

  8. PAT Advanced 1007 Maximum Subsequence Sum

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

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

随机推荐

  1. DVWA全级别之CSRF(跨站请求伪造)

    CSRF(Cross-site request forgery)  CSRF,全称Cross-site request forgery,翻译过来就是跨站请求伪造,是指利用受害者尚未失效的身份认证信息( ...

  2. B1027 打印沙漏

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805294251491328 1027 打印沙漏 (20 分) ...

  3. windows_环境变量

    今天在windows下安装了openSSH,就想在windows下也添加一个服务器地址的环境变量,添加是会了,引用却不会,网上也没找到相应的文章,就自己看了看别人的bat程序,然后找到了引用变量的方法 ...

  4. [人物存档]【AI少女】【捏脸数据】人物鉴赏190

    AISChaF_20200201011129905.png AISChaF_20200123004135233.png

  5. MyEcplise中编码格式的修改问题

    1.如果是在Run Configurations中修改编码格式的话,只能是修改当前java文件的编码格式,把改文件中的代码复制到 另一新建 的java文件中会出现异常,所以就会出现相同的代码在两个不同 ...

  6. sudo用户找不到环境变量 sudo找不到/usr/local/bin 下的执行文件,

    出于安全方面的考虑,使用sudo执行命令将在一个最小化的环境中执行,环境变量都重置成默认状态. 所以PATH这个变量不包括用户自定义设置的内容,如找不到/usr/local/bin/下面的命令在sud ...

  7. python SMTP发邮件

    # from email.mime.text import MIMEText from email.header import Header import smtplib # sender = 'zc ...

  8. Whctf - OLDDRIVER - Writeup

    Whctf - OLDDRIVER - Writeup 转载请标明出处http://www.cnblogs.com/WangAoBo/p/7541536.html 题目: 分析: 给了10组RSA的加 ...

  9. Linux - seq

    1. 概述 生成序列的命令 之前貌似写过 seq 命令, 这次单独拉出来再写一次吧 节操先放一放 2. 命令 1. 帮助 命令 # 输出我就不打了, # 这个命令可用, 证明组件可用 > seq ...

  10. Docker - CentOS 7 安装

    1. 概述 安装 docker markdown 显示有点问题 代码块里的  后面应该跟一个换行, 但是没有跟 这样会导致部分命令直接执行没有反应 2. 环境 os CentOS7 用户 root 3 ...