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. 在RAC中,当私有网线拔了后,会怎么样?

    原文链接http://blog.mchz.com.cn/?p=4305 实际环境: OS:oel5.5_x64 ORACLE:10205 3节点rac 架设于vmware esxi虚拟机上 所需测试项 ...

  2. HDU 4738 Caocao's Bridges

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. MFC学习 socket

    下面未处理异常 tcpserver.cpp #include "WinSock2.h" #include <stdio.h> #pragma comment(lib, ...

  4. gem5 运行x86全系统仿真

    使用gem5可以启动Linux内核,称为全系统模拟,启动之后,可以通过telent连接,进行访问,但四telent有时不稳定,gem5推荐使用m5term进行连接访问,整个步骤如下: (1)打开终端, ...

  5. Web 站点的水平扩展和垂直扩展 (译文)

    当一个开发人员提升计算机系统负荷时,通常会考虑两种方式垂直扩展和水平扩展.选 用哪种策略主要依赖于要解决的问题以及系统资源的限制.在这篇文章中我们将讲述这两种策略并讨论每种策越的优缺点.如果你已经有一 ...

  6. 学习资料 50个常用的sql语句

    Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题 ...

  7. MFC TOOLBAR

    m_imagelist.Create(,,ILC_COLOR24|ILC_MASK,,); CBitmap bmp; ;i<;i++) { int a= bmp.LoadBitmapW(IDB_ ...

  8. iOS 根据字符串来定位地址

    - (void)viewDidLoad { [super viewDidLoad]; self.geocoder = [[CLGeocoder alloc]init]; // 设置地图可缩放 self ...

  9. WWF3自定义活动<第八篇>

    WWF提供了对原有活动进行扩展以及自定义新活动的功能,用户可以通过"Workflow Activity Library"创建和开发自定义活动. 一.自定义活动类型 默认情况下,创建 ...

  10. 【MySQL】frm文件解析

    官网说明:http://dev.mysql.com/doc/internals/en/frm-file-format.html frm是MySQL表结构定义文件,通常frm文件是不会损坏的,但是如果出 ...