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 ...
随机推荐
- php 移动操作数组函数
下面的几个主要是移动数组指针和压入弹出数组元素的和个函数. 函数 功能 array_shift 弹出数组中的第一个元素 array_unshift 在数组的开始处压入元素 array_push 向数组 ...
- bootstrap的pillbox使用
使用bootstrap的cameo模版,搭建了一个cms系统,使用pillbox做显示的时候,出现点击×失败的问题. 分析了一下pillbox这个控件的使用方法. pillbox的样例在cameo/f ...
- python操作oracle完整教程
1. 连接对象 操作数据库之前,首先要建立数据库连接.有下面几个方法进行连接. >>>import cx_Oracle>>>db = cx_Oracle.co ...
- Python格式化字符串知多少
字符串格式化相当于字符串模板.也就是说,如果一个字符串有一部分是固定的,而另一部分是动态变化的,那么就可以将固定的部分做成模板,然后那些动态变化的部分使用字符串格式化操作符(%) 替换.如一句问候语: ...
- Java代码三级跳——表达式、语句和代码块
Java代码三级跳—表达式.语句和代码块 表达式(expression):Java中最基本的一个运算.比如一个加法运算表达式.1+2是一个表达式,a+b也是. 语句(statement):类似于平时说 ...
- jvm01
hotspot:是jvm的核心组件(或者名称),jvm 需要对class文件进行编译成cpu能直接运行的代码.hotspot会对频繁使用的class代码进行缓存,不会再次编译,类似于缓存 client ...
- Flex布局如何实现最后一个元素右对齐(CSS)
在最后一项元素使用样式: margin-left: auto;
- Codeforces Round #622 (Div. 2) A. Fast Food Restaurant
Tired of boring office work, Denis decided to open a fast food restaurant. On the first day he made ...
- js禁止原生手机返回键(物理返回键)
$(document).ready(function() { if (window.history && window.history.pushState) { $(window).o ...
- P1598
无语的是,我以为题目条件的‘在任何一行末尾不要打印不需要的多余空格’意思是每一行都只能到最后一个 '*' 出现就换行,然后用了 '\b',结果怎么都不过,于是看了题解,发现别人都没管这个 = =!!, ...