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 ...
随机推荐
- windows 2012 R2安装SqlServer2016提示缺少KB2919355
补丁的安装顺序如下: 1, 首先安装 Windows2012R2更新的先决条件KB2919442.2,按照如下顺序安装后续KB文件.顺序:clearcompressionflag.exe.KB2919 ...
- 笔试题目练习-python
以下内容包含笔试练习库的题目和代码,题目来自牛客网,仅供参考. # coding = utf-8 import sys def test1(): """ 题目描述:计算字 ...
- Vue.js教程--基础2(事件处理 表单输入绑定
事件处理 表单输入绑定 事件处理 监听v-on 监听 DOM 事件,并在触发时运行一些 JavaScript 代码. 可以在v-on:click=''加内联语句. 有时也需要在内联语句处理器中访问原始 ...
- shell中引号的妙用
#!/bin/bashfile=('leon 01.cap' leon-02.cap nicky-01.cap whoareu-01.cap 8dbb-01.cap)dict=(simple.txt ...
- Report CodeForces - 631C (栈)
题目链接 题目大意:给定序列, 给定若干操作, 每次操作将$[1,r]$元素升序或降序排列, 求操作完序列 首先可以发现对最后结果有影响的序列$r$一定非增, 并且是升序降序交替的 可以用单调栈维护这 ...
- Elections CodeForces - 1020C (贪心)
大意: 有n个选民, m个党派, 第i个选民初始投$p_i$一票, 可以花费$c_i$改变投票, 求最少花费使得第一个党派的票数严格最大 假设最终第一个党派得票数$x$, 枚举$x$, 则对于所有票数 ...
- ubuntu svn二进制文件
1. 查找2:04时间的日志文件和position. Ps:这里假设我找到的是 mysql-bin.000065 位置开始为1356. 2 复制最近的几个日志文件,从mysql-bin.000065 ...
- thinkphp if标签
1.thinkphp框架中的if标签,用于html页面中.在html中编写php代码 1).从控制器中得到数据在循环中if else 判断:<volist name="system_r ...
- C# 中的时间(DataTime)
在做报表或查询的时候,常常会预设一些可选的日期范围,如本周.本月.本年等,利用 C# 内置的DateTime基本上都可以实现这些功能. 当前时间: DateTime dt = DateTime.Now ...
- POJ 1442 splay
前几天用treap写了这一题,不过treap支持的操作不如splay的多,作为一个完美主义者,重新用splay写了这一题. splay大部分操作可以通过 强大到无与伦比的数据结构splay-tree ...