最大子列和CT 01-复杂度2 Maximum Subsequence Sum
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
#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的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- 01-复杂度2 Maximum Subsequence Sum
01-复杂度2 Maximum Subsequence Sum (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...
- 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 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 ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
随机推荐
- YUV主要采样格式理解
主要的采样格式有YCbCr 4:2:0.YCbCr 4:2:2.YCbCr 4:1:1和 YCbCr 4:4:4.其中YCbCr 4:1:1 比较常用,其含义为:每个点保存一个 8bit 的亮度值(也 ...
- 你不知道的JavaScript 二
词法作用域 上次说到作用域,将其定义为一套规则,这套规则用来管理引擎如何在当前作用 域以及嵌套的子作用域中根据标识符名称进行变量查找. 作用域共有两种主要的工作模型.第一种是最为普遍的,被大多数编程语 ...
- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
转自:http://www.linuxidc.com/Linux/2012-05/60966.htm 在工作中,每次部署应用时都需要从本机Windows服务器拷贝文件到Linux上,有时还将Linux ...
- php操作mysql的基础链接实例
- JavaScript内置对象
对象概述 JavaScript是一种基于对象的脚本语句,而不是面向对象的编程语言.对象就是客观世界存在的实体,具有属性和方法两方面特性. 访问对象的属性和方法的方式如下: 对象名.属性 对象名.方法名 ...
- Testing and Checking Refined
还是James大叔的文章:http://www.satisfice.com/blog/archives/856 本文提出了Testing和checking的定义和他们之间的区别. ========== ...
- JavaScript数据结构,队列和栈
在JavaScript中为数组封装了大量的方法,比如:concat,pop,push,unshift,shift,forEach等,下面我将使用JavaScript提供的这些方法,实现队列和栈的操作. ...
- 学习练习 Oracle数据库小题 Students
- CreateThread和_BeginThread的区别
1.程序: 程序构成: (1)源代码 (2)可执行的二进制代码 程序是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念.由操作系统加载其可执行的二进制代码,分配相应的数据结构:进程控制 ...
- rel="stylesheet" 描述
<link type="text/css" rel="stylesheet" href="css/style.css"/> re ...