【DP-最大子串和】PAT1007. Maximum Subsequence Sum
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
/*
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的更多相关文章
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 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(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
随机推荐
- (转) s-video vs. composite video vs. component video 几种视频格式详细说明和比较
之前对着几种视频格式认识不是很清晰,所以看数据手册的时候,看的也是稀里糊涂的. 因为项目中需要用到cvbs做视频输入,在元器件选型上,看到tw2867的数据手册上,有这么一句话: The TW2867 ...
- Unity3D Shader基础教程
原文地址:http://bbs.9ria.com/thread-212557-1-1.html 此教程将指引你如何建立自己的Shaders,让你的游戏场景看起来更好.Unity配备了强大的阴影和材料的 ...
- VS2012:升级Update2和Update3均告损坏,重装方法
My steps (after backing up everything before deleting it): 1. Uninstalled VS2012 Pro from Windows 8 ...
- Lynx
http://www.unlinux.com/doc/soft/20051105/7402.html http://www.today-wx.com/linux/274.html 把 HTML 转成文 ...
- 空基类优化—— EBCO—— empty base class optimization
完全参考自:<C++ Templates The Complete Guide>e_CN,p_281 16.2 空基类优化 最近周围有点吵,论文没看进去,随便翻了本书…… 下文没有多大意义 ...
- .NET 4并行编程入门之Task的取消[转]
原文:http://www.cnblogs.com/Leo_wl/archive/2010/06/01/1749596.html前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并 ...
- Unable to resolve target 'android-9'
右键项目文件--->properties--->android 选择对应版本 保存 如还不生效 打开项目文件project.properties ,修改 target=android-1 ...
- mysql触发器应用和创建表错误代码: 1118 Row size too large. 解决
1.针对数据库查询问题的方便,可以建立重要表的log备份记录表,在主表的添加,修改,删除添加触发器,修改触发器增加触发字段的点,限制条件. 数据库log表查问题比从线上多台服务器上下载日志文件相对方便 ...
- sublime text--你所不知道的12个秘密
转自:http://blog.csdn.net/laokdidiao/article/details/51429404 package control安装: 第一种方法是在控制台中复制粘贴代码按回车, ...
- linux环境中安装ftp服务
需求说明: 今天项目中有一个新的需求,需要在linux环境中搭建一个ftp服务,在此记录下. 操作过程: 1.通过yum的方式安装ftp服务对应的软件包 [root@testvm01 ~]# yum ...