Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 154155    Accepted Submission(s): 35958

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
 
Sample Output
Case 1:
14 1 4

Case 2:
7 1 6

 
 
 
 

最大连续子序列

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20109    Accepted Submission(s): 8884

Problem Description
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., 
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个, 
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和 
为20。 
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 
子序列的第一个和最后一个元素。
 
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
 
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。 
 
Sample Input
6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
 
Sample Output
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0

Hint

Hint

Huge input, scanf is recommended.

 
 
 
两题的思路都差不多,假设现在只有s[0]一个元素,现要添加一个元素s[1],那么s[1]要么是新串的起点,要么是原串暂时的终点。如果之前的串的最大和小于0,那么s[1]的值加上原串之后只会小于s[1]本身,所以索性不加,s[1]自己新开一个串,自己作为起点。如果之前的串的最大和大于等于0,那么s[1]就增加到这个串上,并且暂时成为该串的终点。所以每加入一个元素,要么更新起点,要么更新暂时的终点。可以用一个数组DP[i]来保存以[i]为终点的子串的最大值,每次试图更新最大值即可。
 
 
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100005 int main(void)
{
int t,n,count;
int dp[MAX];
int max,start,START,end;
count = ; scanf("%d",&t);
while(t --)
{
count ++; scanf("%d",&n);
for(int i = ;i < n;i ++)
scanf("%d",&dp[i]); max = dp[];
start = START = end = ; for(int i = ;i < n;i ++)
{
if(dp[i - ] < && dp[i - ] != dp[i]) //讨论dp[i-1]小于0和大于等于0两种情况即可,后面的条件是为了符合题意
start = i + ; //更新起点
else if(dp[i - ] >= )
dp[i] = dp[i - ] + dp[i]; //隐式地更新终点 if(max < dp[i])
{
START = start;
max = dp[i];
end = i + ;
}
}
printf("Case %d:\n",count);
printf("%d %d %d\n",max,START,end);
if(t)
puts("");
} return ;
}

max sum

上面的代码我用了两个循环,下面这个版本只用了一个,速度反而没第一个快,不知为何。

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100005 int main(void)
{
int t,n,count;
int dp[MAX];
int max,start,START,end;
count = ; scanf("%d",&t);
while(t --)
{
count ++; scanf("%d",&n);
for(int i = ;i < n;i ++) //在读入的时候就顺便处理,不知为何会更慢
{
scanf("%d",&dp[i]);
if(!i)
{
max = dp[];
start = START = end = ;
}
else if(dp[i - ] < && dp[i - ] != dp[i])
start = i + ;
else if(dp[i - ] >= )
dp[i] = dp[i - ] + dp[i]; if(max < dp[i])
{
START = start;
max = dp[i];
end = i + ;
}
} printf("Case %d:\n",count);
printf("%d %d %d\n",max,START,end);
if(t)
puts("");
} return ;
}

max sum_2

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10005 int main(void)
{
int k;
int dp[MAX],s[MAX],max,max_start,max_end,start; while(scanf("%d",&k) && k)
{
for(int i = ;i < k;i ++)
scanf("%d",&s[i]); max = s[];
dp[] = s[];
max_start = max_end = start = ; for(int i = ;i < k;i ++)
{
if(dp[i - ] < && s[i] != dp[i - ]) //一样的讨论是否为负就行了
{
start = i;
dp[i] = s[i];
}
else if(dp[i - ] >= )
dp[i] = dp[i - ] + s[i]; if(dp[i] > max)
{
max = dp[i];
max_start = start;
max_end = i;
}
} if(max < )
{
max = ;
max_start = ;
max_end = k - ;
}
printf("%d %d %d\n",max,s[max_start],s[max_end]);
} return ;
}

最大连续子序列

这题还有下面这个版本,就是用个双重循环来选出起点和终点,然后就算这个区间的值,可以用一个循环算出以1为起点的值,然后再计算的时候就可以用这个数组推出来,感觉挺不错的,也有DP的思想在里面,虽然超时了。

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10005 int main(void)
{
int k,i,j;
long long s[MAX],dp[MAX],box,max,max_i,max_j; while(scanf("%d",&k) && k)
{
scanf("%lld",&dp[]);
s[] = dp[];
for(int i = ;i < k;i ++)
{
scanf("%lld",&dp[i]);
s[i] = dp[i];
dp[i] += dp[i - ]; //DP[i]保存以1为起点i为终点的区间的值
} max = dp[];
max_i = max_j = ;
for(int i = ;i < k;i ++)
for(int j = i;j < k;j ++)
{
if(i)
box = dp[j] - dp[i - ]; //i...j区间的值等于1...j的值减去1...i-1的值
else
box = dp[j]; if(box > max)
{
max = box;
max_i = i;
max_j = j;
}
} if(max < )
{
max = ;
max_i = ;
max_j = k - ;
}
printf("%lld %lld %lld\n",max,s[max_i],s[max_j]);
} return ;
}

最大连续子序列_2

 
 

HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)的更多相关文章

  1. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  4. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  6. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  7. HDU 1003 Max Sum (动规)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. hdu 1003 Max sum(简单DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem ...

  9. HDU 1003 Max Sum 解题报告

    题目大意:求一串数字中,几个连续数字加起来最大值,并确定起始和最末的位置. 思路:这是一题DP题,但是可以用尺取法来做.我一开始不会,也是看了某大神的代码,然后有人告诉我这是尺取法,现在会了. //尺 ...

随机推荐

  1. C++的辅助工具介绍 [转]

    C++的辅助工具介绍 1 文档类  (1) Doxygen  参考站点:http://www.doxygen.org  Doxygen是一种适合C风格语言(如C++.C.IDL.Java甚至包括C#和 ...

  2. 28.怎样在Swift中实现单例?

    1.回忆一下OC中的单例实现 //AFNetworkReachabilityManager中的单例,省略了其他代码 @interface AFNetworkReachabilityManager : ...

  3. win10的安装、win10启动盘制作

    需要的材料 win10映像 U盘 UltraISO软件 1.下载对应的win10映像 有64位和32位可选(自己找地方下) 2.下载UltraISO软件 3.准备一只U盘,插入电脑 4.启动Ultra ...

  4. tab栏切换的特殊效果(类似网易的登陆栏效果)

    代码显示效果如上图所示: 需求说明: 在实际需求中,会遇到这样的情况:不仅是要展示选项卡的内容,而且还有可能在选项卡中需求顾客填写相关内容,而这些内容是顾客如果想了解更深层次的,就会继续填写右边的内容 ...

  5. Rstudio安装

    1.https://www.r-project.org/下载R语言(注意32位还是46位系统). 2.安装R,尽量默认安装路径,安装路径不要有中文. 3.https://www.rstudio.com ...

  6. 解决Unable to connect to a repository at URL 禁止访问 (forbidden)

    连接SVN报如下错误. Unable to connect to a repository at URL 禁止访问 (forbidden) 1.         右键点击本地副本,TortoiseSV ...

  7. Hadoop on Mac with IntelliJ IDEA - 7 解决failed to report status for 600 seconds. Killing!问题

    本文讲述作业在Hadoop 1.2.1完成map后ruduce阶段遇到failed to report status for 600 seconds. Killing!问题的解决过程. 环境:Mac ...

  8. WaitForMultipleObject与MsgWaitForMultipleObjects用法

    http://blog.csdn.net/byxdaz/article/details/5638680 用户模式的线程同步机制效率高,如果需要考虑线程同步问题,应该首先考虑用户模式的线程同步方法. 但 ...

  9. Lockless Ring Buffer Design

    https://www.kernel.org/doc/Documentation/trace/ring-buffer-design.txt Lockless Ring Buffer Design == ...

  10. Android中string.xml文件中设置部分字体颜色大小

    1.在string.xml文件中: <string name="tips_all"><Data><![CDATA[清理进程:<font colo ...