Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } 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

 #include <stdio.h>
int MaxSubSum(int a[],int n);
int FirstI,EndI;
int main()
{
int n,a[],MaxSum;
while(scanf("%d",&n)!=EOF)
{
for(int i = ;i < n;i++)
scanf("%d",&a[i]);
MaxSum = MaxSubSum(a,n);
if(MaxSum != -)
printf("%d %d %d\n",MaxSum,a[FirstI],a[EndI]);
else
printf("0 %d %d\n",a[FirstI],a[EndI]);
}
return ;
}
int MaxSubSum(int a[],int n)
{
int ThisSum = ,MaxSum= -;
int firsti=-;
FirstI = ;EndI = n-;
for(int i = ;i < n;i++){
ThisSum += a[i];
if(MaxSum < ThisSum){
MaxSum = ThisSum;
EndI = i;
FirstI = firsti + ;
}
else if(ThisSum < )
{
ThisSum = ;
firsti = i;
}
}
return MaxSum;
}

忘掉以前写过了,又写一遍。。。然而总体思路好像差不多 但是这二次有一项不通过。。。记录下(已改正)上下两个想法一模一样,不愧是亲生的。。。

 //并列和对应相同i,不同j,即尾是0   该项不通过
#include <stdio.h>
int left = ,right = ,right_left = ;
int MaxSubseqSum4(int a[],int size)
{
int ThisSum = ,MaxSum = -; //MaxSum = -1是为了只有负数和 0时,记录第一个0位置
for(int i = ;i < size;i++){
ThisSum += a[i];
if(MaxSum < ThisSum) {
MaxSum = ThisSum;
right = i;
right_left = left; //因为l之后ThisSum < 0时left会变 把此时right对应的left记录下来
}
else if(ThisSum < ) {
ThisSum = ;
left = left + 1;      //原为left++ 此处出错 left+1为了记录最大子列的第一个的位置
}  
}
return MaxSum;
}
int main()
{
int n,a[]; scanf("%d",&n);
for(int i = ;i < n; i++) {
scanf("%d",&a[i]);
}
int MaxSum = MaxSubseqSum4(a,n); if(MaxSum == -1) { //全是负数       //原为left == n 上面错了 这里也错了
printf("0 %d %d\n", a[], a[n-]);
} else {
printf("%d %d %d\n", MaxSum, a[right_left], a[right]);
}
return ;
}

最大子列和CT 01-复杂度2 Maximum Subsequence Sum的更多相关文章

  1. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  2. 01-复杂度2 Maximum Subsequence Sum

    01-复杂度2 Maximum Subsequence Sum   (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...

  3. 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  4. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

  5. 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  6. 01-复杂度2 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  7. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

  8. 01-复杂度2 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  9. PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

随机推荐

  1. Goldengate常用命令

    1.Goldengate的起停 启动goldengate   a> 启动goldengate时最好先从target节点开始,然后是source节点.否则data pump进程可能会由于没有收到t ...

  2. SDUT 3346 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  3. xcode7.3 升级 xcode8.0 后权限设置问题(升级xcode 8.0 后构建版本不显示问题)

    xcode7.3 升级 xcode8.0 后权限设置问题(升级xcode 8.0 后构建版本不显示问题) 前两天为了适配 iOS10 的系统 我将xcode 7.3 升级到了 xcode 8.0 但是 ...

  4. 游戏服务器生成全局唯一ID的几种方法

    在服务器系统开发时,为了适应数据大并发的请求,我们往往需要对数据进行异步存储,特别是在做分布式系统时,这个时候就不能等待插入数据库返回了取自动id了,而是需要在插入数据库之前生成一个全局的唯一id,使 ...

  5. Debian的一个命令

    dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包.下面是它的一些命令解释:1)安装软件命令行:dpkg -i <.deb file name> ...

  6. 将U盘分成 启动盘+文件存储区

    我看了很多帖子,发现想要将U盘分区的朋友绝大部分是和我一样,想用U盘做成一个启动盘同时兼顾文件存储,分区的目的很简单,就是想将启动部分单独做成一个区,以免在日常的应用中使得启动文件染毒或者误操作造成损 ...

  7. SQL函数——LENGTH()和LENGTHB()

    LENGTH()函数是比较简单同时也是非常有用的一个函数,在此小记一下,加深印象以备后用! 1:先上实验的例子——我新建了一张Student表,插入了若干条测试数据,如下图所示: 2:实验LENGTH ...

  8. 两个一样的tomcat不能同时启动解决方法

    环境:两个Tomcat7.0.20,使用一个JVM,JDK7,Win7 64位系统. 1.使用压缩版的tomcat不能使用安装版的. 2.第一个tomcat的配置不变. 3.增加环境变量CATALIN ...

  9. javascript设计模式-适配器模式

    适配器模式的主要作用是将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些对象(类)可以一起工作. UML示意图: 例如,鸭子有fly方法和quack(嘎嘎 ...

  10. Duilib学习笔记《02》— 界面布局

    1. 界面描述XML文件 Duilib主要是通过XML来进行界面的布局配置,程序通过读取并解析XML文件来创建对应的窗体.DuiLib的页面布局分为三类:窗体(Window).容器(Contain)和 ...