hdu 1003
| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
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
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 题意不说 和hdu1231很相似 刚刚写了1231,但被这题坑了 maxsum我开始初值设了0 一直wa
后面测试数据才发现自己错了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
typedef long long ll;
#define N 100005
using namespace std;
int a[N];
int b[N];
ll dp[N];
int main()
{
int t;
int n;
int i,j;
int first,next;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
ll maxsum=-99999999;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp));
first=1;
for(i=1;i<=n;i++)
{
if(dp[i-1]+a[i]>=a[i])
dp[i]=dp[i-1]+a[i];
else
{
dp[i]=a[i];
first=i;
}
b[i]=first;
maxsum=max(maxsum,dp[i]);
}
for(i=1;i<=n;i++)
{
if(maxsum==dp[i])
{
next=i;
//break;
}
}
printf("Case %d:\n",j);
if(j!=t)
printf("%I64d %d %d\n\n",maxsum,b[next],next);
else
printf("%I64d %d %d\n",maxsum,b[next],next);
}
return 0;
}
hdu 1003的更多相关文章
- dp 动态规划 hdu 1003 1087
动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...
- 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(A - 最大子段和)
HDU 1003(A - 最大子段和) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A 题目: ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)
问题描述: 连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...
- hdu 1003 hdu 1231 最大连续子序列【dp】
HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...
- HDU 1003 动态规划
http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...
- HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1003 最大连续子段和
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)M ...
随机推荐
- Show "Appear Offline" in Lync
在注册表中 HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Communicator 创建DWORD,name是EnableAppearOffline,v ...
- 慕课网-安卓工程师初养成-6-5 使用循环操作 Java 中的数组
来源:http://www.imooc.com/code/1531 实际开发中我们经常使用循环控制数组成员的操作.如: 运行结果: 其中, 用于获取数组的长度 需要注意的“小毛病”: 1. 数组下标 ...
- vi的查找与替换
0x01 查找 (在命令行模式下) /<要查找的字符> 向下查找要查找的字符 ?<要查找的字符> 向上查找要查找的字符 0x02 替换 (在底行模式下) :0,$s/s ...
- animate.css配合wow.min.js实现各种页面滚动效果
有的页面在向下滚动的时候,有些元素会产生细小的动画效果.虽然动画比较小,但却能吸引你的注意.比如刚刚发布的 iPhone 6 的页面(查看).如果你希望你的页面也更加有趣,那么你可以试试 WOW.js ...
- FizzlerEx —— 另一个HtmlAgilityPack的CSS选择器扩展,
之前我介绍过HtmlAgilityPack的CSS选择器扩展——ScrapySharp,它可以非常方便的实现通过CSS选择器表达式来查询HtmlNode.今天在使用的过程中,发现它不支持nth-chi ...
- 使用linux的nc来进行文件的传输
NAME nc - arbitrary TCP and UDP connections and listensSYNOPSIS nc [-46DdhklnrStUuvz] [-i interv ...
- 调用Ria Service中方法的各种方式
前端界面后台: using System; using System.Collections.Generic; using System.Linq; using System.Net; using S ...
- 种类并查集(POJ 1703)
1703 -- Find them, Catch them http://poj.org/problem?id=1703 题目大意:有2个敌对帮派,输入D a b表示a,b在不同帮派,输入A a b表 ...
- <a>标签的四种状态
a:link a:visited a:hover a:active
- crontab这个坑,执行单个命令执行成功,但是写到crontab怎么也不行
昨天开始,一直在弄通过定时任务控制熄屏,在终端下的单个命令可以执行成功,写到脚本里,单个执行脚本能成功,但是写到crontab定时任务,怎么也不熄屏,但他确实执行了,看日志,是一直报错的:百度了很多很 ...