HDU1003MAX SUM
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 206582 Accepted Submission(s):
48294
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.
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).
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.
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+;
int a[maxn],n;
int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int sum=,ans=-;
int s=,e=,k=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
if(sum>ans)
{
s=k;
e=i;
ans=sum;
}
if(sum<) //0的意义就是这段数做的是负功
{
sum=;
k=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",ans,s,e);
if(t>) puts("");
}
return ;
}
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N],n;
int main()
{
int t,cas=;
cin>>t;
while(t--)
{
cin>>n;
int sum=,mx=-,s=,e=,ts=;
for(int i=;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
if(sum>mx)
{
mx=sum;
s=ts;
e=i;
}
if(sum<)
{
sum=;
ts=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",mx,s,e);
if(t) printf("\n");
}
return ;
}
/*
100
2 1 2
1 1
3 -1 1 2
2 -7 3
*/
牢记顺序是 加 大于 小于!!
HDU1003MAX SUM的更多相关文章
- HDU1003MAX SUM (动态规划求最大子序列的和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 动态规划:HDU1003-Max Sum(最大子序列和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 动态规划: HDU1003Max Sum
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- C++-HDU1003-Max Sum
时间复杂度O(n) 空间复杂度O(1) #include <cstdio> int main() { int T;scanf("%d",&T); ,n,a,l, ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
随机推荐
- 【BZOJ-1965】SHUFFLE 洗牌 快速幂 + 拓展欧几里德
1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 541 Solved: 326[Submit][St ...
- javaScript基础练习题-下拉框制作(JQuery)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 用WinRAR进行安装包的制作
简单的绿色的安装包制作工具,如果不想用复杂且庞大的vs提供的制作工具,或许这个绿色解压安装包是个不错的选择. 下面我收集了一些制作的教程(百度经验的文章)和一些常用到的命令行: WinRAR自解压安装 ...
- C++ map 映照容器
map映照容器的元素数据是一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系. map映照容器的数据结构是采用红黑树来实现的,插入键值的元素不允许重复,比较函数只对元素的键值进行比较, ...
- 初学Hibernate持久化
hibernate三种持久化对象状态:(持久化对象:Persistent Object=POJO + hbm映射) 1.瞬时状态(临时状态或自由态):PO对象刚创建(即new)开始进入瞬时状态,此时对 ...
- Get与Post比较
- 一分钟认识GitHub
一分钟认识GitHub 机缘巧合之下,我开始使用 GitHub ,但事实上,刚刚入门的我不知道如何才能使 GitHub 发挥他的全部功能.不久之前,我偶然听了李福斯先生对 GitHub 的介绍,受益匪 ...
- localdb链接字符串
<add name="GitCandyContext" connectionString="Data Source=(localdb)\v11.0;Integrat ...
- epoch iteration batchsize
深度学习中经常看到epoch. iteration和batchsize,下面按自己的理解说说这三个的区别: (1)batchsize:批大小.在深度学习中,一般采用SGD训练,即每次训练在训练集中取b ...
- ThinkPHP3.2 行为扩展以及插件机制介绍!
首先行为扩展这个概念是TP架构的核心组成之一,关于行为的解释我就粗略的概括一下吧:TP在从接受到HTTP请求到最终将视图输出,期间经历的很多步骤,这些步骤大家可以在http://document.th ...