1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  1. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  2. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  8. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  9. 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

随机推荐

  1. windows phone 换肤(2)

    //这里有篇参考文章 http://www.cnblogs.com/tianhonghui/p/3373276.html#commentform 以下思路是来自徐老师,昨晚看了一个晚上球赛,睡了不到6 ...

  2. Unity5.5.2 CD旋转 顺时针逆时针

    UGUI 下  Sprite_CD  在Inspector下  Image(Script) 下  Clock wise  勾选  决定  CD是顺时针还是逆时针  默认是顺时针  勾选则为逆时针

  3. dubbo 安装部署Windows

    1 安装zookeeper 2 安装dubbo    1 下载源码 https://github.com/alibaba/dubbo 2 编译 mvn clean package install -D ...

  4. C#时常需要调用C++DLL

    在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传递和返回字符串是,现总结一下,分享给大家: VC++中主要字符串类型为:LPSTR,LPCSTR, LPCTSTR, st ...

  5. 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)

    七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...

  6. node.js安装以及git 的使用说明

     第一步:安装node.js: Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.   Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高 ...

  7. 关于pip无法安装scrapy的问题

    安装scrapy时如果出现下列问题: building ' twisted. test. raiser' extension error: Microsoft Visual C++ 14.0 is r ...

  8. 15、OpenCV Python 轮廓发现

    __author__ = "WSX" import cv2 as cv import numpy as np # 基于拓扑结构来发现和绘制(边缘提取) # cv.findConto ...

  9. 在Pd中取消Code Name 同步

    以前记得现在忘记了,好不容易找回来,记住备忘吧.  

  10. N1 Armbian 安装 Domoticz

    前言 N1 中安装 Domoticz 的方法与这篇类似,MQTT 服务器改用 mosquitto,更轻量级. 步骤 安装 Domoticz,只选择 HTTP 8080 端口 curl -sSL ins ...