【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 ...
随机推荐
- Linux CPU Load Average
理解Linux系统负荷 LINUX下CPU Load Average的一点研究 Linux load average负载量分析与解决思路 Understanding Linux CPU Load - ...
- android项目引入三方类库配置文件
android项目中可能会用到诸多外部的三方库,如**.jar或者引用第三个项目,那么它们引用的这些东西都放在哪里呢?我们来看下. 如果引入的是三方的jar包,我们默认的是放在了libs文件夹下,然后 ...
- Windows2008安装WebSphere 6.1提示此安装程序不能在图形方式中运行
推荐的解决方式:http://blog.sina.com.cn/s/blog_4b010fb50100n1mk.html 在一般情况下,安装Websphere是直接运行launchpad.exe,然后 ...
- YII2 设置session过期时间
设置session过期时间 如何在YII里设置SESSION过期时间,而不需要在php.ini里面设置. 在protected/config/main.php里,设置: 代码如下 复制代码 'comp ...
- Linux-centos6.8下关闭防火墙
一.临时关闭防火墙 1. 查看防火墙的状态 [root@vpnSS ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ...
- mocha框架下,异步测试代码错误造成的问题----用例超时错误
今天用抹茶(mocha)做个测试,发现有一个测试项目总是超时: describe("DbFactory functions",function(){ it("query ...
- json字符串使用注意问题
json本身是字符串,即 json字符串 js使用 要把 json字符串 转为 javascript对象 json字符串转为js对象的方法:jquery的parseJSON var str='[{& ...
- 在mvc4中多语言建站的实例
环境:vs2012 asp.net mvc4. 实现方式:resource 资源文件,根据路由规则中Lang参数来判断载入哪种语言方式 在网上找到了相关资料,顺便自己做了个练习,新建工程之类的步骤就免 ...
- 针对降质模型中的模糊SR
(PDF) Deep Plug-and-Play Super-Resolution for Arbitrary Blur Kernels https://www.researchgate.net/pu ...
- 用highcharts展现你的数据
摘要: 前面已经分享过图表插件,今天在来将下如何使用highcharts来绘制图表.highcharts支持在线定制,你可以选择你所需要的模块,然后点击build就会生成一个js文件链接,右键保存到本 ...