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. 【设计和开发一套简单自己主动化UI框架】

    !有兴趣的朋友请直接移步Github,本帖子已经不做更新,框架的详细的实现已经做了优化和代码整理,本文仅仅介绍了详细的设计思路! 目标:编写一个简单通用UI框架用于管理页面和完毕导航跳转 终于的实现效 ...

  2. python_smtplib

    import smtplib smtpserver = 'smtp.qq.com' fromaddr = 'fromaddr@qq.com' toaddrs = 'toaddr@qq.com' msg ...

  3. mybatis plus 联合查询

    在xml中只需要需要写如下的代码即可实现分页: <select id="selectUserList" parameterType="map" resul ...

  4. Ansible 之Playbook

    ansbile playbook是一系列ansible命令的集合,利用yaml 语言编写,playbook命令根据自上而下的顺序依次执行.同时,playbook开创了很多特性,它可以允许你传输某个命令 ...

  5. MyBatis通用Mapper技巧

    一.排序 错误代码:example.orderBy(BaseEntity.Field.GMTUpdate + " desc"); 正确方式: 一是:通过注解 @OrderBy(va ...

  6. 精心挑选的HTML5/CSS3应用及源码

    这段时间我已经为大家分享了不少关于HTML5应用和jQuery插件了,先来回顾一下: 炫酷霸气的HTML5/jQuery应用及源码 干货分享 超炫丽的HTML5/jQuery应用及代码 绚丽而实用的j ...

  7. u3d fpsCounter

    因为u3d自己的stats下面的fpscounter不是实际意义上的fps,所以看到demo的fpsCounter,把它改写为c#的 using UnityEngine;using System.Co ...

  8. 解决win8.1电脑无法安装手机驱动问题

    相信安装过win8.1的朋友都会有个问题,那就是自己的安卓手机怎么都连不上电脑,比如360助手.豌豆荚.91助手,都安不上驱动.下面几个简单步骤即可轻松解决. 1.首先声明,官方的驱动是完全支持win ...

  9. 找不到 android-support-v4 解决办法

    Project->properties->Java Build Path->Libraries->Add External Jars中加入sdk目录下的extras/andro ...

  10. Github上搭建个人博客记录

    1.注册,用户名一定要起好,别随便起. 2.登录后,新建一个仓库repositories.new一个. 命名为用户名.github.io.如果发现不一样进Settings修改,rename. 3.仓库 ...