1007 Maximum Subsequence Sum (25分)

 

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } 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

题意:
  求连续区间最大和,并输出这个区间左右端点的值 题解:
  1、模拟,求出每一个正数的连续区间和,更新最大区间值并记录左右端点的值
  2、dp[i]表示区间以a[i]结尾的区间和,转移方程为dp[i]=max(dp[i],dp[i-1]+a[i]),并更新区间下标
注意:
  1、当a[i]全为负值的时候,输出0 a[0] a[n-1]
  2、
  当输入为:
  5
  -1 -1 0 -1 -1
  输出为:0 0 0
 
代码一(模拟)
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<map>
#define MAX 1000000
#define ll long long
using namespace std;
int a[];
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
int ans=-,l=,r=n-;
int temp=,first=;
for(int i=;i<n;i++)
{
temp=temp+a[i];
if(temp<)
{
temp=;
first=i+;
}
else if(temp>ans)
{
ans=temp;
l=first;
r=i;
}
}
if(ans<)
ans=;
cout<<ans<<' '<<a[l]<<' '<<a[r]<<endl;
return ;
}

代码二(dp)
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<map>
#define MAX 1000000
#define ll long long
using namespace std;
int a[],l[],r[];
int dp[];//以a[i]结尾的最大连续区间和
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
dp[i]=a[i];//初始化
} l[]=r[]=; for(int i=;i<n;i++)
{
if(dp[i-]+a[i]>=dp[i])//如果a[i]>=0
{
dp[i]=dp[i-]+a[i];
l[i]=l[i-];//区间左端点不变,右端点更新
r[i]=i;
}
else//如果a[i]是负数,新建一个区间
{
l[i]=i;
r[i]=i;
}
}
int ans=-,pos=,flag=;
for(int i=;i<n;i++)
{
if(dp[i]>ans)
{
flag=;
ans=dp[i];
pos=i;
}
}
//cout<<flag<<endl;
if(flag==)
cout<<<<' '<<a[]<<' '<<a[n-]<<endl;
else
cout<<ans<<' '<<a[l[pos]]<<' '<<a[r[pos]]<<endl;
return ;
}
 

1007 Maximum Subsequence Sum (25分) 求最大连续区间和的更多相关文章

  1. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (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. 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)

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

  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 (25 分)

    题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...

  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. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

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

  9. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

随机推荐

  1. Linux - XShell - alt 快捷键的设置

    1. 概述 命令行的 alt 快捷键可能会冲突 2. 环境 os win10 centos7 xshell xhell6 3. 场景 开启 centos7 虚拟机 在 win10 打开 xshell6 ...

  2. python之 '随机'

    Q:想生成随机数,用哪个库? import random Q:想生成一个随机整数,范围在[0, 100]之内,怎么弄? >>> random.randint(0, 100) 7 Q: ...

  3. GIT分布式代码管理系统

    1:GTI介绍及使用 环境搭建: 服务器 IP地址 主机名 角色 Centos7.5 192.168.200.113 gitserver GIT服务器 Centos7.5 192.168.200.11 ...

  4. django+vue基础框架:django one对one格式

    创建app:python manage.py startapp  app01(这里的app01是指名字,可以是a或b等等) 生成迁移文件:python manage.py makemigrations ...

  5. 《如何增加资本约见》---创业学习---训练营第四课---HHR---

    一,开始 1,思考题: (1)一句话和安利文两份材料怎么准备? (2)接触资本的渠道有哪些? 二,一句话介绍: 1,优秀案列: (1)通过第一视角,服务某某行业的智能AR眼镜: (2)成立三个月GMV ...

  6. 微信小程序获取unionid

    链接:https://blog.csdn.net/a493001894/article/details/80323403

  7. Python(五) 迭代器(Iterable/Iterator/iter())

    原文的链接:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143178254 ...

  8. java语法基础--动手动脑问题及课后实验问题

    ---恢复内容开始--- 动手动脑: 1:仔细阅读示例:EnumTest.java,运行它,分析运行结果 结果 :枚举类型是引用类型!枚举类型不属于原始数据类型,它的每个具体指都引用一个特定的对象.相 ...

  9. python实现基于百度路径规划接口的坐标对获取两点驾车距离的计算

    今天为大家介绍一种通过python实现坐标对间距离数据的获取方法.接口采用百度开发的路径规划接口. 1.调用接口: 接口:(传入起点坐标串,结束坐标串:ak值需要注册百度开发者) 接口详细说明 htt ...

  10. (2)Linux Java环境变量安装

    install default JRE/JDK Installing Java with apt-get is easy. First, update the package index: sudo ...