最大子列和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 ...
随机推荐
- Android——getSystemService
android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardServi ...
- Android——Gallery 图片拖动效果
1.XML文件 <?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android=&q ...
- Kendo UI - Class 基类定义
在 kendo 中,使用原型继承机制,Class 是 Kendo 中的基类,定义了函数 extend 用来派生其它类. function Class() {} Class.extend = funct ...
- 调用WEKA包进行kmeans聚类(java)
所用数据文件:data1.txt @RELATION data1 @ATTRIBUTE one REAL @ATTRIBUTE two REAL @DATA 0.184000 0.482000 0.1 ...
- adb 命令模拟按键事件 模拟 点击 事件
有时我们需要程序模拟按钮或点击,而手机本身又没有,哪么可以采取adb 模拟实现,最后再去实际设备去测试(前期一般都拿不到设备): 如模拟上一首,下一首,暂停等,手机上是没有的,但有些设备上是有的: / ...
- AngularJs $http.post 数据后台获取不到数据问题 的解决过程
第一次使用 AngularJs 的 $http 模块的时候,遇到过后台获取不到前台提交数据的问题,检查代码没有发现问题,先上代码. js 代码 angular.module("newsApp ...
- 使用OpenCV/python进行双目测距
在做SLAM时,希望用到深度图来辅助生成场景,所以要构建立体视觉,在这里使用OpenCV的Stereo库和python来进行双目立体视觉的图像处理. 立体标定 应用标定数据 转换成深度图 标定 在开始 ...
- 获取oracle 里的表名与字段
--数据库表名 SELECT distinct A.OBJECT_NAME as TAB_NAME,B.comments as DESCR FROM USER_OBJECTS A , USER_TAB ...
- (转)各种排序算法的分析及java实现
转自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 排序一直以来都是让我很头疼的事,以前上<数据结构>打酱油去了,整个学期下来才勉强 ...
- 【MySQL】使用mysqlbinlog回滚
参考:http://wubx.net/?s=mysqlbinlog mysql官方的mysqlbinlog没有回滚的功能,淘宝大牛对官方代码进行了修改使之能够将binlog中的DML操作变成互逆的语句 ...