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. e620. Activating a Keystroke When Any Component in the Window Has Focus

    Normally, a keystroke registered to a component is activated when the component has the focus. This ...

  2. c# 递归函数使用案例

    /// <summary> /// 递归查询 /// </summary> /// <param name="groupID"></par ...

  3. SSL 证书服务推荐

    最近要用到ssl.故做了一些搜索 1.Let's Encrypt:免费,快捷,支持多域名(不是通配符),三条命令即时签署+导出证书.缺点是暂时只有三个月有效期,到期需续签. 2.StartSSL免费D ...

  4. linux中chown命令

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...

  5. Peckham添加引用文件模糊匹配智能提示

    下载地址:https://github.com/markohlebar/Peckham ​ 跟VVDocumenter规范注释生成器的安装方式一样: 下载开源工程在Xcode重新编译运行会自动安装此插 ...

  6. Hopewell Project Sharing项目总结分享PPT

    这篇随笔记录的是2013年底,Hopewell Project已经成功验收后,开项目分享会所编写的PPT. 由于此项目是本人带领Team成员一起开发,而且关键技术是自己把控,所以公司希望能开个项目分享 ...

  7. 【DeepLearning】汉字手写体识别

    http://blog.topspeedsnail.com/archives/10897 两个双卷积池化 + dropout + 2个全链接 + softmax

  8. Python中使用MongoEngine

    pymongo来操作MongoDB数据库,但是直接把对于数据库的操作代码都写在脚本中,这会让应用的代码耦合性太强,而且不利于代码的优化管理 一般应用都是使用MVC框架来设计的,为了更好地维持MVC结构 ...

  9. centos7系统下nginx安装并配置开机自启动操作

    准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...

  10. LED驱动程序分析

    混杂设备 LED驱动程序分析 /******************************* * *杂项设备驱动:miscdevice *majior=10; * * *************** ...