动态规划:HDU1003-Max Sum(最大子序列和)
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 81735 Accepted Submission(s): 18797
1000).
end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
14 1 4
Case 2:
7 1 6
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
int num[maxn],dp[maxn];
int Start,End,now_start,Max;
int main()
{
int t;
int T;
scanf("%d",&t);
T = t;
while(t--)
{
memset(dp,0,sizeof(dp));
num[0] = -1;
now_start = 1;
Max = -1000000;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
{
if(num[i] + dp[i-1] >= num[i])//前面的数不是负数
dp[i] = num[i] + dp[i-1];
else
{
dp[i] = num[i];//前面一个数是负数,将现在这个数作为新的起点
now_start = i;//当前起点,当出现最大值的用得到
}
if(dp[i] >= Max)//出现最大值,记录最大值,终点和起点
{
Start = now_start;
End = i;
Max = dp[i];
}
}
printf("Case %d:\n",T-t);
if(t != 0)
printf("%d %d %d\n\n",Max,Start,End);
else
printf("%d %d %d\n",Max,Start,End);
}
return 0;
}
动态规划:HDU1003-Max Sum(最大子序列和)的更多相关文章
- [ACM_动态规划] hdu1003 Max Sum [最大连续子串和]
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾
2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...
- HDU-1003 Max Sum(动态规划,最长字段和问题)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)
Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...
- 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)
最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...
- HDU--1003 Max Sum(最大连续子序列和)
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- hdu1003 Max Sum(经典dp )
A - 最大子段和 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descr ...
- HDU1003 Max Sum(求最大字段和)
事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...
- (动态规划)Max Sum Plus Plus--hdu--1024
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Othe ...
- hdu1003 Max Sum【最大连续子序列之和】
题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...
随机推荐
- js 限制文本框不能输入单引号
<input onkeydown="if(event.keyCode==32||event.keyCode==188||event.keyCode==222){return false ...
- C# 多线程之线程控制
方案一: 调用线程控制方法.启动:Thread.Start();停止:Thread.Abort();暂停:Thread.Suspend();继续:Thread.Resume(); private vo ...
- 谷歌地图自定义popup框
谷歌地图的infowindow 不提供官方的定制化 <!DOCTYPE html> <html> <head> <meta name="viewpo ...
- android中开启线程
其实Android启动线程和JAVA一样有两种方式,一种是直接Thread类的start方法,也就是一般写一个自己的类来继承Thread类.另外一种方式其实和这个差不多啊! 那就是Runnable接口 ...
- 从零开始的全栈工程师——js篇2.1(js开篇)
JS开篇 一.js介绍 全称 javascript 但不是java 他是一门前台语言 而java是后台语言js作者 布兰登·艾奇 前台语言:运行在客户端的后台语言:跟数据库有关的 能干什么? 页 ...
- windows 下设置MTU数值
输入:netsh interface ipv4 show subinterfaces 查询到目前系统的MTU值.再分别输入一行按一次回车键. netsh interface ipv4 set subi ...
- 改善WPF应用程序性能的10大方法 (转发)
WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...
- zabbix-3.4-服务监控
服务监控 总览 服务监控(services monitoring)旨在帮助那些想要高级(业务)基础设施的监控的人.在许多情况下,我们关注的不是底层细节,比如磁盘空间不足.CPU 负载高等.我们关注的是 ...
- 进程加载与segment
elf文件是一组结构体和数据的组合. elf文件是一种文件格式,这种格式定义了进程加载器如何读取elf文件的内容. elf文件的程序头或者segment对如何加载(读取)做了说明.
- numpy.random.shuffle(x)的用法
numpy.random.shuffle(x) Modify a sequence in-place by shuffling its contents. Parameters: x : array_ ...