HDU 1003:Max Sum(DP,连续子段和)
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 292444 Accepted Submission(s): 69379
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
题意
给出一个数组,求连续子段和的最大值
思路
第一次写用的前缀和,写完后发现复杂度太高了,交上去果断TLE
翻了一下以前写的51Nod的一道连续字段和的题,写了出来。复杂度O(n)
首先定义一个变量sum,使sum的值足够小,然后开始输入元素x,如果sum<0(即sum+x<x),将sum的值变为x,起点变为当前x的位置(相当于舍弃了前面的sum的值,从x开始重新累加)。然后定义ans,first,end分别记录最终的最大值,起点,终点。sum每改变一次,让ans和sum进行比较,如果ans<sum,更新ans的值,并将起点first为l,终点end变为当前x的位置i,最后输出即可。
一篇关于最大连续子序列和的讲的很不错的博客:https://blog.csdn.net/samjustin1/article/details/52043369。里面还有好多种写法,很详细
AC代码
#include<bits/stdc++.h>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=1e6+10;
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
int _=0;
while(t--)
{
int n;
cin>>n;
int m;
ll ans=INT_MIN;
ll sum=INT_MIN;
int l;
// sum,sum为临时记录最大值和起点位置的变量
int first,end;
for(int i=1;i<=n;i++)
{
cin>>m;
//如果sum小于0,让sum从m重新开始
//并改变l的值
if(sum+m<m)
{
sum=m;
l=i;
}
else
sum+=m;
// 如果ans小于sum,更新ans,first,end
if(ans<sum)
{
first=l;
end=i;
ans=sum;
}
}
cout<<"Case "<<++_<<':'<<endl;
cout<<ans<<" "<<first<<" "<<end<<endl;
// 注意输出。最后一组样例中没有空行,前面每组之间都有空行
if(t)
cout<<endl;
}
return 0;
}
HDU 1003:Max Sum(DP,连续子段和)的更多相关文章
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- hdu 1003 Max Sum (DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others) ...
- [ACM] hdu 1003 Max Sum(最大子段和模型)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- hdu 1003 Max sum(简单DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
- HDU 1003 Max Sum(DP)
点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...
- hdu 1003 Max Sum(基础dp)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu 1024 Max Sum Plus Plus (子段和最大问题)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- 小橙书阅读指南(十三)——连通性算法(union-find)
上一章我大概说明了什么是图论以及无向图的基础概念,本章我们要研究一种更普遍的算法——连通性算法.它属于图论的分支,也是一种抽象算法.在深入算法之前,我们先提出一个具体的问题:假设在空间中存在N个点,我 ...
- webpack和tree shaking和rollup
http://blog.csdn.net/haodawang/article/details/77199980 tree shaking只对es模块生效,在打包tyscript模块是要使用tsc编译器 ...
- linux使用flock文件锁
使用linux flock 文件锁实现任务锁定,解决冲突 格式: flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command flock ...
- 20170813xlVBA跨表筛选数据
一.数组方案 Sub CustomFilter() Dim Rng As Range, Arr As Variant Dim EndRow As Long, EndCol As Long Dim i ...
- 20170706xlVBA根据工资汇总表生成个人工资条
Sub NextSeven20170706001() Application.ScreenUpdating = False Application.DisplayAlerts = False Appl ...
- JS-Object(3) 继承(prototype方式, 类方式); javascript6的知识(部分)
原型方式的继承 创建child object classes(constructors) , 子类如何从父类中继承特性. 原型链继承prototypal inheritance (ruby中的继承也是 ...
- Android Studio 常用快捷键和使用技巧
Android Studio 1.Ctrl+E,可以显示最近编辑的文件列表 2.Shift+Click可以关闭文件 3.Ctrl+[或]可以跳到大括号的开头结尾 4.Ctrl+Shift+Backsp ...
- Leha and another game about graph CodeForces - 840B (dfs)
链接 大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值 首先要注 ...
- The requested URL /phpmyadmin was not found on this server.
这个报错,我弄了好久,第一次我以为我安装有问题,我就卸载重新安装了,但是在结果还是报这样子的错. 查找phpmyadmin的安装位置输入: sudo dpkg -L phpmyadmin 可以看到很多 ...
- char,varchar与text类型的区别和选用
(1)char: 它是定长格式的,但是长度范围是0~255. 当你想要储存一个长度不足255的字符时,mysql会用空格来填充剩下的字符.因此在读取数据时,char类型的数据要进行处理,把后面的空格去 ...