hdu 1003,nefu 728 max sum
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 //前1个数的DP值>=0,就加;DP值<0,就重新开始,自己本身。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring> using namespace std;
int data[],dp[]; int main()
{
int c,num=,n;
cin>>c;
while(c--)
{
num++;
int maxx=-,a=,b=;
memset(dp,,sizeof(dp));
cin>>n;
for(int i=;i<n;i++)
scanf("%d",&data[i]);
dp[]=data[];
for(int i=;i<n;i++)
{
if(dp[i-]>=)
{
dp[i]=dp[i-]+data[i];
}
else
{
dp[i]=data[i];
a=i;
}
if(dp[i]>maxx)
{
maxx=dp[i];
b=i;
}
}
printf("Case %d:\n",num);
printf("%d %d %d\n",maxx,a+,b+);
}
return ;
}
hdu 1003,nefu 728 max sum的更多相关文章
- hdu 3415(单调队列) Max Sum of Max-K-sub-sequence
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3415 大意是给出一个有n个数字的环状序列,让你求一个和最大的连续子序列.这个连续子序列的长度小于等于k. ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Max Sum -- hdu -- 1003
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 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) ...
- 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)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
随机推荐
- c# 读取ACCESS 数据库
using System; using System.Collections.Generic; using System.Data.OleDb; using System.IO; using Syst ...
- Foxit Reader(福昕PDF阅读器) v4.3.1.218 绿色专业版
软件名称:Foxit Reader(福昕PDF阅读器) v4.3.1.218 绿色专业版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 4.40MB 图片 ...
- iOS10 CAAnimationDelegate的适配
最近在xcode8打开之前的动画代码,看到如下警告
- CentOS 6.5安装PostgreSQL9.3.5时报错: jade: Command not found
CentOS 6.5安装PostgreSQL9.3.5时报错: jade: Command not found 1[root@pghost1 postgresql-9.3.5]# ./configur ...
- 接口速度慢问题查找(TTFB时间长)
前些天自己写了一个网站,但是发现接口的速度按超级慢,业务逻辑并不复杂,原因究竟在哪呢? 首先说一下,我的数据库和项目均在同一台服务器上,按道理来说,接口访问本地的数据库应该会很快才对. 后来我发现线上 ...
- 学习Java第一篇——Java 安装及环境搭配
内容提要: 1.下载JDK: 2.安装JDK: 3.配置JDK; 第一,下载JDK: 1.登陆网址:www.oracle.com 2.点击 Downloads 3.选择 Java SE 4.选择 ...
- curl 命令使用总结
curl 查看网页源码 curl www.sina.com 保存页面 -o curl -o [文件名] www.sina.com 直接在curl命令后加上网址,就可以看到网页源码. 如果要把这个网页保 ...
- AtomicInteger的用法
J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...
- 配置 Apache 的虚拟主机
1.在host配置比如: 找到记事本以管理员的身份打开,然后文件->打开 C:\Windows\System32\drivers\etc 下面的hosts文件 127.0.0.1 www ...
- 使用JS通过正则限制input的输入
第一: 限制只能是整数 type = "text" name= "number" id = 'number' onkeyup= "if(! /^d+$ ...