链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415

意甲冠军:环。要找出当中9长度小于等于K的和最大的子段。

思路:不能採用最暴力的枚举。题目的数据量是10^5,O(N^2)的枚举回去超时。本题採用的非常巧妙的DP做法,是用单调队列优化的DP。

运用的是STL的deque,从i:1~a找到以当中以i为尾的符合条件的子段。并将i本身放入双向队列。全部i从队列后放入,保证了队列的单调性。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define maxn 100005*2
#define maxm
#define INF 0x7fffffff
typedef long long ll;
using namespace std;
int num[maxn],sum[maxn];
int main()
{
int tot;
scanf("%d",&tot);
while(tot--)
{
int a,b;
scanf("%d%d",&a,&b);
scanf("%d",&num[1]);
sum[1]=num[1];
for(int i=2;i<=a;i++)
{
scanf("%d",&num[i]);
sum[i]=sum[i-1]+num[i];
}
for(int i=a+1;i<a+b;i++)
sum[i]=sum[i-1]+num[i-a];
deque < int > dd;
int ans=-INF,head=-1,tail=-1;
for(int i=1;i<a+b;i++)
{
while(!dd.empty()&&sum[i-1]<sum[dd.back()])
dd.pop_back();
while(!dd.empty()&&i>dd.front()+b)
dd.pop_front();
dd.push_back(i-1);
if(sum[i]-sum[dd.front()]>ans)
{
ans=sum[i]-sum[dd.front()];
head=dd.front()+1;
tail=i;
}
}
if(head>a)
head-=a;
if(tail>a)
tail-=a;
printf("%d %d %d\n",ans,head,tail);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 3415 Max Sum of Max-K-sub-sequence 最长K子段和的更多相关文章

  1. HDU 1081:To The Max

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

  2. 【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]

    [题解]最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052] 传送门:最大 \(M\) 子段和 \(Max\) \(Sum\) \(Plus\) \(Plu ...

  3. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

  4. hdu 3415 Max Sum of Max-K-sub-sequence 单调队列。

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)

    转载请注明出处:http://blog.csdn.net/u012860063 Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java ...

  6. hdu 3415(单调队列) Max Sum of Max-K-sub-sequence

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3415 大意是给出一个有n个数字的环状序列,让你求一个和最大的连续子序列.这个连续子序列的长度小于等于k. ...

  7. HDU 3415 Max Sum of Max-K-sub-sequence【单调队列】

    <题目链接> 题目大意: 给你一段从1~N的圆形序列,要你求出这段圆形序列中长度不超过K的最大连续子序列之和是多少,并且输出这子序列的起点和终点. 解题分析: 既然是求连续子序列之和,我们 ...

  8. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. HDU 1024:Max Sum Plus Plus(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

  10. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

随机推荐

  1. [C/C++]_[VS2010来源与UTF8中国字符串转码ANSI问题]

    现场: 1.思想vs设置源文件UTF8编码,代码中国串出现在它必须是utf8编码,不幸的是,,假定源代码将出现在中国字符串,在存储器中转码ANSI编码. Unicode(UTF8签名) 代码页(650 ...

  2. Swift学习资料

    http://www.hangge.com/blog/cache/category_72_1.html

  3. Git显示漂亮日志的小技巧

    Git的传统log如下所示,你喜欢吗? 看看下面这个你喜不喜欢?(点击图片看大图) 要做到这样,命令行如下: 1 git log --graph --pretty=format:'%Cred%h%Cr ...

  4. [Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]

    原文:[Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]  我的文章一定要做到对读者负责,否则就是失败的文章  -- ...

  5. nginx随着passenger构造ruby on rails页

    1.备份nginx简介 cp /opt/nginx/html/nginx.conf /opt/nginx/html/nginx.conf.bak 2.编者nginx简介 server { listen ...

  6. Cordova探险系列(一个)

    最早接触PhoneGap平台是在1年多之前,可以使用HTML.CSS和JavaScript跨平台来编写Android或者IOS设备程序.而且应用的核心代码不须要多少改动就行移植.确实让我感觉的到它应该 ...

  7. Linux-2.6.32内核编译流量计数器nfacct

    最近一直想看到一本书<一个Jiegeng华>.而技术不依赖书.但是,这并不表示我IT技术没有兴趣.事实证明,,当我无法理解的沧桑.肮脏的领导者无法理解的心理.自我可惜无法理解它处处感受到脏 ...

  8. [译]MVC应用程序生命周期

    原文:MVC Application Lifecycle 来一探究竟在MVC应用程序中参与请求处理的各个不同组件. 目录: 序言 背景 UrlRoutingModule RouteHandler Mv ...

  9. SQLServer数据类型优先级对性能的影响

    原文:SQLServer数据类型优先级对性能的影响 译自: http://www.mssqltips.com/sqlservertip/2749/sql-server-data-type-preced ...

  10. ubuntu,从一个新用户,要转到新用户的命令行操作

    shibo-ubuntu@ubuntu:~$ sudo useradd karen [sudo] password for shibo-ubuntu:  shibo-ubuntu@ubuntu:~$ ...