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. 制作Win7(x86)PE ISO文件

    WinPE3.1     —Win7 x86 PE V3.1: waik_supplement_zh-cn.isoDVD: cn_windows_7_professional_with_sp1_x86 ...

  2. 使用 Cocos2d-x 3.1.1 创建 Windows Phone 8 游戏开发环境

    cocos2d-x 是目前流行的游戏游戏开发框架,目前最新的版本是 3.1.1, 网上有些教程已经比较老了,本文将会介绍如何使用最新的 3.1.1 创建 Windows Phone 8 开发环境. 本 ...

  3. Python和Django在Windows上的环境搭建

    作为一个.NET程序员,真心不喜欢Python以及PHP这种松散的语法.有人说,程序员应该多学几门语言,本想学习Java,无奈感觉Java的语法太啰嗦了.很多人都推荐Python,说它的语法简洁,执行 ...

  4. 【caffe-windows】 caffe-master 之 cifar10 超详细

    本教程尽量详细,大多步骤都有图,如果运行出错,请先对照自己的文件是否和图上的一样,包括标点啊,空格啊,斜杠,反斜杠啊之类的小细节. 本例程是在 win10 64位   caffe-master     ...

  5. university, school, college, department, institute的区别

    这些个词没有太大区别,有时候有些词是可以通用的,而有些用法则是随着地域时间的不同而变迁. 一般说来,college译作“学院”,它是university (综合性大学)的一个组成部分,例如,一所综合大 ...

  6. Drupal常用开发工具(一)——Devel模块

    进行 Drupal 开发时有许多模块和工具可供使用,其中最常用的两项便是 Devel 及 Drupal for Firebug.本文和<Drupal常用开发工具(二)——Drupal for F ...

  7. tomcat下部署可以访问的文件夹

    项目名#文件夹名#文件夹名.xml <Context docBase="D:\文件夹名\r文件夹名"/> 例如: test#zhang#test1.xml <Co ...

  8. 【翻译习作】 Windows Workflow Foundation程序开发-第一章01

    第 1 章    欢迎来到工作流的世界 …思想如蝴蝶般飞到我身边 —— Gossard / Vedder (译注:Gossard与Vedder是来自Pearl Jam乐队的2名乐手,该句出自他们的歌曲 ...

  9. QTP 场景恢复– 函数调用

    创建自动化测试是为了实现无人值守下运行,但也给开发人员带来一些问题.假如你离开办公室前启动测试,想要让它通宵运行.然而,由于不可预见的错误,您的测试会在某一点停止,中断了测试结果.因此QTP中引入场景 ...

  10. Java基础学习(学习IT企业必读的324个JAVA面试题.pdf 整理)

    一.Java程序基础 javac 文件名.java    编译程序 java 类名               运行java程序 代码规范中,一下几点要注意: 包名:包名是全小写的名词,中间可以由点分 ...