pat1007. 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
方法一:
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;
#define size 10000
int line[size+];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int k;
int i,j,l,fl,fr;
bool t=false;
scanf("%d",&k);
for(i=;i<k;i++){
scanf("%d",&line[i]);
if(line[i]>=){
t=true;
}
}
/*if(!t){
cout<<0<<" "<<line[0]<<" "<<line[k-1]<<endl;
return 0;
}*/
long long tempmax=line[],fmax=line[];
l=;
fl=fr=; //这句话没加 case3(从0开始)过不了
line[k]=;
for(i=;i<=k;i++){
if(tempmax>=){
if(tempmax>fmax){
fmax=tempmax;
fl=l;
fr=i-;
}
}else{
tempmax=;
l=i;
}
tempmax+=line[i];
}
if(fmax<){
fmax=;
fl=;
fr=k-;
}
cout<<fmax<<" "<<line[fl]<<" "<<line[fr]<<endl;
return ;
}
方法二要比方法三简明,更容易理解。
方法二:
#include <stdio.h>
#include <string.h>
int num[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j=;
for(i=;i<n;i++){
scanf("%d",&num[i]);
if(num[i]<){
j++;
}
}
if(j==n){
printf("%d %d %d\n",,num[],num[n-]);
}
else{
int tempsum=;
int tempi=,tempj;
int sum,l,r;
sum=l=;
r=n-;
for(i=;i<n;i++){
if(tempsum>=){
tempsum+=num[tempj=i];
}else{
tempsum=num[tempi=tempj=i];
}
if(tempsum>sum||(tempsum==&&r==n-)){//注意单个0的情况
sum=tempsum;
l=tempi;
r=tempj;
}
}
printf("%d %d %d\n",sum,num[l],num[r]);
}
return ;
}
方法三:
#include <stdio.h>
#include <string.h>
int num[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j=;
for(i=;i<n;i++){
scanf("%d",&num[i]);
if(num[i]<){
j++;
}
}
if(j==n){
printf("%d %d %d\n",,num[],num[n-]);
}
else{
num[n]=;
int tempsum=;
int tempi,tempj;
int sum,l,r;
sum=l=;
r=n-;
for(i=;i<=n;i++){
if(tempsum>){
if(tempsum>sum){//当i元素前面的局部和>0时,有可能更新
sum=tempsum;
l=tempi;
r=tempj;
}
tempsum+=num[tempj=i];
}else{//否则,i元素前面的局部和<=0
if(tempsum==&&sum==&&tempi==tempj&&l!=r){//i元素前面的局部和==0,并且局部和只有一个元素,并且最终和未更新,则更新最新和
l=r=tempi;
tempsum+=num[tempj=i];
}
else{//其他情况,一律不更新
tempi=tempj=i;
tempsum=num[i];
}
}
}
printf("%d %d %d\n",sum,num[l],num[r]);
}
return ;
}
pat1007. Maximum Subsequence Sum (25)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- 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 ...
- 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 ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
随机推荐
- unity中播放视频
unity中播放视频步骤如下: 1.将要播放的视频拖入projec.(注意:unity一般支持的视频格式有mov, .mpg, .mpeg, .mp4,.avi, .asf格式 ) 2.在场景中添加 ...
- Castle Windsor
让我们从Web API的集成点开始,它们是IDependencyResolver和IDependencyScope接口.IDependencyResolver和其他接口的名称可能与MVC中的接口相同, ...
- angular ng-content
<p> child works! </p> <ng-content></ng-content> <app-child> 父组件投影 < ...
- Flask写web时cookie的处理
本文来自网易云社区 作者:孙圣翔 flask是一个微型web开发框架,别看他微型,在github上排名还是挺高的. A microframework based on Werkzeug, Jinja2 ...
- C# LINQ(8)
回顾之前的代码都是LINQ自行推断类型.其实LINQ在查询的结束是可以动态创建类型. , , , , , , , , , , }; var list = from num in intArray &a ...
- 《Think in Java》
chapter 1 对象导论 面向对象程序设计(Object-oriented Programming ,OOP) chapter 2 一切都是对象 字段和方法 若类的某个成员变量是基本数据类型,即是 ...
- 842. Split Array into Fibonacci Sequence
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- zookeeper安装和使用 windows
的 Zookeeper 是以 3.4.5 这个稳定版本为基础,最新的版本可以通过官网 http://hadoop.apache.org/zookeeper/来获取,Zookeeper 的安装非常简单, ...
- go的三个常用命令go run go build go install
go的三个常用命令 go run go build go install 命令源码文件:含有 main函数 的文件 库源码文件:不包含 main函数 的文件, 主要用于编译成静态文件.a供其他包调用 ...
- GDI绘图写的简单扫雷
由于没话多少时间,这个扫雷我只实现了主要功能(扫雷功能,递归实现) 废话不多说,直接上代码 using System; using System.Collections.Generic; using ...