1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分)
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
题意:
求连续区间最大和,并输出这个区间左右端点的值
题解:
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分) 求最大连续区间和的更多相关文章
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
- 【PAT甲级】1007 Maximum Subsequence Sum (25 分)
题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...
- 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 ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- 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 ...
- 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 ...
随机推荐
- ALSA lib基本概念
1.channel 通道,即我们熟知的声道数.左/右声道,5.1channel等等 2.sample A sample is a single value that describes the amp ...
- 开源一个JAVA开发的分类信息源码
最近有空用JAVA折腾的一个分类广告源码. 开发放言:JAVA 框架:SpringMVC Hibernate 分布式用重量级EJB 3.0 实现,可以分布JBOSS部署. 前端用 JSP JQUERY ...
- SSHException: Error reading SSH protocol banner
当我在使用ssh 远程connect 另一台机器的server 时出现了错误,错误如下,起初以为是自己代码写的有问题,后来本地了一下看了跑的没问题,我就开始根据报错去查寻原因, 起初在论坛博客看到这 ...
- Hadoop架构: HDFS中数据块的状态及其切换过程,GS与BGS
该系列总览: Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览 首先,我们要提出HDFS存储特点: 1.高容错 2.一个文件被切成块(新版本默认128MB一个块)在不 ...
- 8.5-Day1T2--Asm.Def 的基本算法
题目大意 给一棵树,求∑∑w_i*w_j*w_LCA(i,j) w_i表示i点权值 题解 显然一点点求lca是肯定会tle的 那就想如何优化 i和j的lca和j和i的lca是一样的 DFS,在每个x处 ...
- spring框架相关概念
软件行业的二八法则?技术中只有20%是最常用和最关键的,决定你的基础,后面的80%决定你的潜能! 概念: 1,轻量级框架,用户需要什么功能就自己添加相应的功能模块,不像重量级框架,一旦用,所有功能都添 ...
- 谈谈一些有趣的CSS题目-- 从倒影说起,谈谈 CSS 继承 inherit
开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你 ...
- Jmeter_接口串联自动化测试_登录后充值获取cookie
1.登陆->充值->运行会报错 2,那如何解决这个问题呢,添加HTTP COokie管理器 另外一种方法,登录->提取正则表达式,充值->添加HTTP cookie管理器
- php.ini修改php上传文件大小限制的方法
打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...
- Vue-设置默认路由选中
需求分析: 一个导航组件,需要其中一个是选中状态,并且样式呈现高亮,选中的导航对应的页面也需要展示出来. 功能实现: router-link内置有一个选中状态,当处于当前路由时,会给 router-l ...