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. Spring - MVC - 修改 Java 类后, 触发重启

    1. 概述 学习 Spring MVC 下, 如何可控的触发重启 2. 背景 学习 Spring 场景 有些时候, 改完类, 需要重启 之前有听说, Spring MVC 可以自动重启 于是想, 尝试 ...

  2. Codeforces Round #610 (Div. 2)E(模拟,DFS)

    先找到一条多边形的边,然后循环一圈输出多边形上的点.把每个三角形看作一个结点,以两个三角形之间公用边为边建立一张图,DFS输出叶子结点,则得到先切后切的顺序. #define HAVE_STRUCT_ ...

  3. 实体间的关系:1:1,1:N,M:N

    *实体之间的关系* 1)1对1关系: 两个实体表内,存在相同的主键字段. 1.1)设计: 如果记录的主键值等于另一个关系表内记录的主键值,则两条 记录对应,1:1对应. 例子: #表一:学生信息表 # ...

  4. eclipse中怎么导入git库下载下来的web项目

    总的看来是有两种方式: 方式一:可以对已经从版本库下载到本地的项目操作(Maven导入) 你可以通过公司提供的内部的版本库的网址登录版本库,之后在里面下载自己想要的那个版本的代码包,见下图 点击右侧的 ...

  5. js相关--浅拷贝和深拷贝

    1.js的数据类型 基本概述:js的数据类型分为两种,分别为基本数据类型和引用数据类型,它们俩的区别在于基本数据类型采用值传递,引用数据类型采用指针形式传递. 如下所示:引用类型通过简单的=进行复制, ...

  6. 吴裕雄 人工智能 java、javascript、HTML5、python、oracle ——智能医疗系统WEB端智能分诊代码简洁版实现

    <%-- Document : getInfo Created on : 2018-10-7, 21:36:37 Author : acer --%> <%@page import= ...

  7. 4500-X启动到“511K bytes of non-volatile configuration memory”,无法继续?

    在设备做了某些配置后,尤其是升级,重启设备后,发现设备在如下界面卡住,无法进一步进入CLI命令行. Platform Manager: starting in standalone mode (act ...

  8. vs2019 opencv4的相关配置

    opencv4.11存在改动,导致许多demo没有办法正常运行,但是配置方法却是相同的. 主要是连接器输入,头文件包含路径,库路.如果想要调试,还需要设置调试文件符号表. [未完待续]

  9. php.ini修改php上传文件大小限制的方法

    打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...

  10. SpringCloud全家桶学习之断路器---Hystrix(五)

    目前我也在摸索着学习Spring Cloud,本节主要摸索的是服务熔断.服务降级.Hystrix服务监控. 一.Hystrix概述 (1)服务雪崩 服务雪崩:多个微服务之间调用的时候,假设微服务A调用 ...