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 ...
随机推荐
- 启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting.
启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting 解决方法: vim /etc/m ...
- 验证码比较hash_equals 方法
验证码是否与缓存中一致时,使用了 hash_equals 方法: hash_equals($verifyData['code'], $request->verification_code) ha ...
- PHP中PHP $_POST和PHP $_REQUEST及PHP $_GET的用法及区别
笔者最近开始学习PHP语言大法,记录一下学习过程中遇到的知识点. 今天介绍的是php中有关 php $_post 和 php $_request 及 php $_get 的用法及区 ...
- 全排列dfs
#include <iostream> #include <vector> using namespace std; vector<int> ans; const ...
- 吴裕雄 python 机器学习——模型选择回归问题性能度量
from sklearn.metrics import mean_absolute_error,mean_squared_error #模型选择回归问题性能度量mean_absolute_error模 ...
- Android学习07
自定义Dialog 创建一个Java类CustomDialog继承Dialog. package com.example.helloworld.widget; import android.app.D ...
- 异常的jvm(java虚拟机)与异常处理try catch与throwable
- 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
转载:https://www.cnblogs.com/tlduck/p/5132738.html #define _WIN32_DCOM #include<iostream> #inclu ...
- Jenkins+Maven+Github+Springboot实现可持续自动部署(非常详细)
目前公司开发的项目已经部署到服务器上,部署项目的测试环境和生产环境,加上每个项目n个服务,于是我就 , 骚就是骚,但是就是太累了,于是花点时间研究了一下Jenkins. Jenkins的作用和它的lo ...
- C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,
//将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换. #include <stdio.h> #include <string.h> void fun ...