1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
32000 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
 /*
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
分析:
首先,既然要记录最大和序列的开始位置、结束位置、最大和以及当前序列的开始位置、结束位置、当前和(不包括当前数),
则分别定义ansst,ansend,ansmax, curst,curend,cursum;
初始化:当前的和=0;
(刚开始不知道这几个变量初始化为多少,先看测试数据;)
接下来,看策略:
1、-10
当前和=0,当前数=-10,因为当前和+当前数=-10<0【4】,显然不可取,因此,我们把当前和=0,当前的开始位置置于下一个下标;
2、1 2 3 4
这些数都是正数【1】,因此,肯定是可取的,这时,就要记录当前结束位置(因为开始位置在第一个数-10的时候已经记录过了,所以不必记录)
当前和累加;因为当前和>最大和,因此,最大和序列的开始,结束位置以及最大和都要更新;
3、-5
负数,显然,这里我们不能去更新最大和序列的一些属性(因为无法判断,但是之前的1,2,3,4序列已经被保存),
注意:1+2+3+4(当前最大和)+(-5)>0【3】,因此,
所以,这里我们要更新的是当前序列的一些属性,结束位置要更新,当前和肯定也要记录(因为可能后面有一个超大正数);
4、-23
此时,cursum=1+2+3+4-5=5,5+(-23)<0【4】,因此,如果加入-23,显然没有之前的大,
而且如果后面有个超大正数,也不可能加上前面的序列(因为他的和是个负数,起了一个削减的作用),因此采用和步骤1相同的策略;
5、3
此时,ansmax=10,cursum=0,curst=7(下标),0+7<10【2】,因此,还无法“动摇”最大和序列,但是,我们已经开始记录结束位置了;
6、7
cursum=3+7=10,10<=10,按照题意,不更新最大和序列,因此,还是选择【2】策略;
7、-21
cursum=10-21<0【4】
经过以上的分析,我们就能写出相应的代码,这时,还有一个问题,就是变量初始化的问题:
如果有这么一组测试数据:
4
-1 -2 0 -1
按照题意显然应该输出:
0 0 0
因此,在当前数为0时,我们应该去更新最大和序列,也就是当前和>最大和【1】,故,ansmax设为负数
再看一组:
2
100 10
显然刚开始就要更新最大和序列,而最大和序列的属性值来源于当前和序列,因此,当前和序列的所有参数值必须全部进行初始化为0;
若当前数为非负数,且加上当前和>最大和,则:1、当前和+=当前数;2、更新最大和序列; 【1】
且加上当前和<=最大和,则:1、当前和+=当前数;2、更新当前序列的结束位置; 【2】
若当前数为负数,且当前和+当前数>0,则:1、当前和+=当前数;2、更新当前序列的结束位置; 【3】
且当前和+当前数<0,则:1、当前和=0;2、更新当前序列的开始位置为下一个下标 【4】
注:这里为什么负数就要和0进行比较,因为加上一个负数,和肯定是减小的,也就是说之前的最大和序列的属性值已经被保存。
*/ #include<iostream>
#include<stdio.h>
using namespace std; #define INF 0x7fffffff int max(int a,int b)
{
return a>b?a:b;
} int data[]; int main()
{
int i;
int n;
int curst,curend,cursum;
int ansst,ansend,ansmax;
cursum=curst=curend=;
ansmax=-;
bool flag=true;
scanf("%d",&n);
for(i=;i<n;++i)
{
scanf("%d",&data[i]);
if(data[i]>=)
{
flag=false;
}
}
if(flag) //全部都是负数
{
printf("0 %d %d\n",data[],data[n-]);
return ;
}
for(i=;i<n;++i)
{
if(data[i]>=)
{
if(cursum+data[i]>ansmax)
{
ansst=curst;
ansend=curend=i;
ansmax=cursum+data[i];
cursum+=data[i];
}
else
{
cursum+=data[i];
curend=i;
}
}
else
{
if(cursum+data[i]>)
{
cursum+=data[i];
curend=i;
}
else
{
curst=i+;
cursum=;
}
}
}
printf("%d %d %d\n",ansmax,data[ansst],data[ansend]);
return ;
}

【DP-最大子串和】PAT1007. Maximum Subsequence Sum的更多相关文章

  1. PAT1007:Maximum Subsequence Sum

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

  2. pat1007. Maximum Subsequence Sum (25)

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

  3. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

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

  4. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

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

  6. PAT 1007 Maximum Subsequence Sum(最长子段和)

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

  7. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

  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. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

随机推荐

  1. JAVA写代码必须知道的编程工具

    Eclipse: 一个开放源代码的.基于Java的可扩展开发平台. NetBeans: 开放源码的Java集成开发环境,适用于各种客户机和Web应用. IntelliJ IDEA: 在代码自动提示.代 ...

  2. MP3帧时长为26ms的来历

  3. ZooKeeper在分布式应用中的作用

    作者:陈叶皓(携程邮轮研发部软件架构师) 是不是要在标题的“作用”之前加上“重要”两个字,我犹豫了一下,zookeeper提供的功能是如此的重要,以至于如果你在应用中不使用它,早晚也会在你的应用中去实 ...

  4. vs 无法启动iis

    visual studio 2013/2015/2017 有时会碰到iis无法启动, 查看系统事件日志发现,缺少 aspnetcore.dll 文件 . 下载此文件,并复制到缺少的目录中,重启visu ...

  5. MySQL5.6主从复制搭建基于日志(binlog)

    什么是MySQL主从复制 简单来说,就是保证主SQL(Master)和从SQL(Slave)的数据是一致性的,向Master插入数据后,Slave会自动从Master把修改的数据同步过来(有一定的延迟 ...

  6. Linux中mkdir和touch命令区别

    一.目的 本文将介绍linux下新建文件或文件夹.删除文件或文件夹命令.         touch能够新建文件,mkdir用来新建文件夹.rm用来删除文件或文件夹.         本文将选取ubu ...

  7. R语言igraph 包-构建网络图

    igaph 是一个项目,目标是建立一条简单,易用的网络分析工具,有 R, python, C/C++ 等语言的具体实现: 项目主页: http://igraph.org/ 在R语言中,对应的就是 ig ...

  8. TensorFlow:tf.train.Saver()模型保存与恢复

    1.保存 将训练好的模型参数保存起来,以便以后进行验证或测试.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf.train.S ...

  9. 编译libjpeg

    本来以为编译libjpeg很容易,结果弄了半天. 先百度了下看下教程,一般是设置path,这里我也做了 我的电脑  -> 属性 -> 高级  -> 环境变量   ,添加环境变量PAT ...

  10. c/c++ 代码中使用sse指令集加速

    使用SSE指令,首先要了解这一类用于进行初始化加载数据以及将暂存器的数据保存到内存相关的指令, 我们知道,大多数SSE指令是使用的xmm0到xmm8的暂存器,那么使用之前,就需要将数据从内存加载到这些 ...