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. C++-POJ1067-取石子游戏

    //(ak,bk)=([k*(1+sqrt(5))/2],[k*(1+sqrt(5))/2]+k)=(ak,ak+k) #include <cstdio> double sqrt5=2.2 ...

  2. 以管理员身份运行 Microsoft Edge 时不支持登录

    之前一直用 edge chromium bate版本 一直不能登录 今天试了正式版 还是这样 然后百度一大堆没找到解决方案 设置兼容性为windown7 是可以的 但是 UI显示会有问题 再次打开 又 ...

  3. 【资源分享】Gmod动态方框透视脚本

    *----------------------------------------------[下载区]----------------------------------------------* ...

  4. 迎娶白富美,走上人生巅峰(python爬虫股票分析)

    一.姿势储备 股票交易,线性代数(分析股票),分位数(风险评估)... finace.yahoo.com 雅虎提供几乎所有股票api www.kaggle.com  有问题找社区 未完待续.....

  5. Linux上后台运行node和springboot服务

    环境:Ubuntu18.04 阿里云云服务器 尝试全局安装forever和pm2均失败,最后以linux自带的nohub启动,以前同样用nohub启动springboot 命令: nohup npm ...

  6. DNS辅助

    DNS服务原理详解 DNS:Domain Name Service,域名解析服务 监听端口:udp/53,tcp/53 应用程序:bind 根域:. 一级域: 组织域:.com, .org, .net ...

  7. php提供下载服务实例

    两个步骤:1,通过header头信息告诉浏览器,我给你回应的是一个附件请接收 2,通过php读取下载的文件的内容并返回 前端 <!DOCTYPE html> <html lang=& ...

  8. IntelliJ IDEA 2017.3尚硅谷-----设置界面

  9. Html学习笔记(二)

    Html头部 HTML <link>元素 <link> 标签定义了文档与外部资源之间的关系. <link> 标签通常用于链接到样式表: <head> & ...

  10. DataGridView编辑后立即更新到数据库的两种方法

    DataGridView控件是微软预先写好的一个显示数据的控件,功能非常强大,可以显示来自数据库表的数据和XML等其他来源的数据. 方法一:基于DataAdapter对象创建一个CommandBuli ...