题意

  需要在o(n)时间内,求最大连续的子序列的和,及其起点和终点。

分析

  一种方法是一边读,一边维护最小的前缀和 s[i] ,然后不断更新 ans = max(ans,s[j] - s[i]),以及起始位置。

  另一种方法是尺取(算是吧),l 和 r 代表起点和终点,一开始l=0,r=1,如果s[r]-s[l]>=0那就往右扫 r++,不断更新  ans = max(ans,s[r] - s[l]),以及起始位置,如果小于0了,那就舍弃前面这段了,也就是后面必然不用考虑它更划算,l=r,r++。还要注意全是负数的情况,那就只要输出最大的那个负数。

代码

维护min(s[i])

#include <cstdio>
#include <algorithm> using namespace std; int test,n;
int a,k,ans,s[],st,en;
int main()
{
scanf("%d",&test);
for(int t=; t<=test; t++)
{
s[]=;
k=;
ans=-;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a);
s[i]=s[i-]+a;
if(s[i]-s[k]>ans){
ans=s[i]-s[k];
en=i;
st=k+;
}
if(s[i]<s[k]){
k=i;
}
}
if(t<test)printf("Case %d:\n%d %d %d\n\n",t,ans,st,en);
else printf("Case %d:\n%d %d %d\n",t,ans,st,en);
}
return ;
}

尺扫

#include <cstdio>
#include <algorithm> using namespace std; int test,n,a,ans,s[],st,en,maxa,maxp;
int main()
{
scanf("%d",&test);
for(int t=; t<=test; t++)
{
maxa=-;
ans=;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a);
s[i]=s[i-]+a;
if(a>maxa)
{
maxa=a;
maxp=i;
}
}
if(maxa<)
{
ans=maxa;
st=en=maxp;
}
else
{
int l=;
int r=;
while(r <= n)
{
while(s[r] >= s[l] && r<n)
{
r++;
if(s[r]-s[l]>ans)
{
ans=s[r]-s[l];
st=l+;
en=r;
}
}
l=r;
r++;
}
}
if(t<test)printf("Case %d:\n%d %d %d\n\n",t,ans,st,en);
else printf("Case %d:\n%d %d %d\n",t,ans,st,en);
}
return ;
}

【HDU 1003】 Max Sum的更多相关文章

  1. 【HDU 5381】 The sum of gcd (子区间的xx和,离线)

    [题目] The sum of gcd Problem Description You have an array A,the length of A is nLet f(l,r)=∑ri=l∑rj= ...

  2. HDU 1003:Max Sum(DP,连续子段和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  3. HDU 1003:Max Sum

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  4. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  5. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  6. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  7. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  8. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  9. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

随机推荐

  1. 使用友盟进行apk的自动更新

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  2. 利用Clip制作打洞效果

    起因 如上篇博文所说,连线原型需要在中间文字上下各留15像素的空白.设计师完成原型之后,问我有没有办法实现,我说,我能想到两种实现方式.其中一种就是上篇博文所说的OpacityMask.第二种就是使用 ...

  3. HTML5之应用缓存---manifest---缓存使用----HTML5的manifest缓存

    相信来查这一类问题的都是遇到问题或者是初学者吧! 没关系相信你认真看过之后就会知道明白的 这是HTML5新加的特性 HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连 ...

  4. 实例化Model的三种方式

  5. zepto的tap事件的穿透分析

    首先是什么情况下会发生zepto(tap)的事件穿透: 当一个弹出层用tap点击之后这个层隐藏或者是移走,都会触发下面对应位置的点击事件(click)和一些标签的默认行为(a标签的跳转.input获取 ...

  6. Java NIO 概述

    Channel 和 Buffer 标准的Java IO编程接口是面向字节流和字符流的 而 NIO 是面向通道和缓冲区的 数据总是从通道中读到Buffer中,或者从Buffer写入通道中 NIO可以理解 ...

  7. codevs 3008 加工生产调度[贪心]

    3008 加工生产调度  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 某工厂收到了n个产品的订 ...

  8. Table AdvanceTable针对表内行禁用multipleSelection , singleSelection

    OATableBean table = (OATableBean)webBean.findChildRecursive("LineTable"); table.setInserta ...

  9. C#操作JSON

    http://www.cnblogs.com/LiZhiW/p/3624729.html C#操作JSON 1. .NET对JSON的支持介绍............................. ...

  10. GEOS库学习之四:几何关系判断

    原理上一篇已经介绍过了,这篇就直接进行程序练习 #include "geos.h" GeometryFactory factory; //创建一条环线,与线的区别就是环线是闭合的. ...