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. C#操作excel打印

    using System; using System.Data; using System.IO; using System.Runtime.InteropServices; using System ...

  2. SSM项目配置文件及其各项使用

    $说明: ·Spring 5  + Mybatis 3.4.5 +SpringMVC  ·使用druid数据库 ·使用log4j输出日志 $Spring 及其配置文件(部分) Spring官方网站:h ...

  3. WebStrom背景色设置

    Ctrl Alt S快速打开setting:

  4. cinder create volume的流程-scheduler调度

    创建 Volume 时,cinder-scheduler 会基于容量.Volume Type 等条件选择出最合适的存储节点,然后让其创建 Volume. 1.cinder-scheduler配置相关项 ...

  5. hive的安装与配置 mysql安装 启动

    三种模式 内嵌模式:元数据保持在内嵌的derby模式,只允许一个会话连接 本地独立模式:在本地安装Mysql,吧元数据放到mySql内 远程模式:元数据放置在远程的Mysql数据库 1.下载Hive安 ...

  6. Jmeter_使用IE代理录制脚本

    因为项目登录的密码需要RSA加密,选用了jmeter作为压测工具: 就自己本次项目,顺便学习Jmeter,做一个简单的记录,本文主要介绍使用IE代理录制脚本: 自己也尝试过使用Badboy录制,还是喜 ...

  7. [Swift实际操作]九、完整实例-(1)在iTunesConnect网站中创建产品

    本文将通过一个实例项目,演示移动应用开发的所有步骤.首先要做的是打开浏览器,并进入[iTunesConnect网站],需要通过它创建一款自己的应用. 在iTunesConnect的登录页面中,输入自己 ...

  8. Nagios监控平台搭建及配置文件详解

    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员, ...

  9. JS 只创建一个元素

    //判断有没有这个元素<div id="div"> if(my$("div").firstElementChild){ console.log(&q ...

  10. 解决CentOS查询不到ip

    问题:登陆操作系统,输入ip addr 也可以输入ifconfig查看ip,发现ens33目录中没有inet属性 解答:查看ens33的网卡配置: vi /etc/sysconfig/network- ...