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, ...
随机推荐
- javascript 文件的操作
js 文件的操作(ActiveXObject仅支持IE) 一.参数解释: 1. filename: filename //文件路径 2.iomode: var forReading=1;只读var f ...
- c++基类指针指向继承类调用继承类函数
类里面重载运算符>>, 需要使用友元函数,而友元函数,不能作为虚函数. 所以,基类指针无法直接调用继承类里重构的 >> ; 使用类转换,能解决掉,基类指针 调用 继承类 ...
- C# 抽象(2)
接着上章说: 先看代码 abstract class Human { public abstract void Think(); public abstract int Age { get; set; ...
- NSURLSession 网络请求
1.NSURLSession 在 iOS9.0 之后,以前使用的 NSURLConnection 过期,苹果推荐使用 NSURLSession 来替换 NSURLConnection 完成网路请求相关 ...
- day08.4-samba共享网盘服务
1. 安装软件:yum install samba -y 2. 新建共享目录物理路径:mkdir /zizaijiapu 修改配置文件:vim /etc/samba/smb.con ...
- 【转】C# String 与 Char[] 数组 相互转换
源地址:http://blog.csdn.net/razilfelix/article/details/52289663 string 转换成 Char[] string ss = "abc ...
- 1. UML统一建模语言
(1)UML概述: 建模: 对现实系统进行适当的过滤, 用适当的表现规则描述出简洁的模型. 建模是一种深入解决问题的方法. UML: UML(United Modeling Language, 统一建 ...
- Xamarin Forms:小马过河,王者归来
因为我媳妇的原因,去年下半年从零开始学习Android原生开发,做了一个答题库app.整体给我的感觉是入门难度不大,前期折腾一番,大部分时间都是花在开发上面,其实任何一门语言都是如此. 今年我又有另一 ...
- (C/C++) Link List - C++ 版本
利用C++寫一個基本的 Link list 練習,功能包含 pint list.CreatList.Insert.Delete.Reverse.Search.Clear.GetLen. 先建立相關的C ...
- [HEOI2012]采花 树状数组 BZOJ 2743
题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...