Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5690    Accepted Submission(s): 2059

Problem Description
Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left neighbour of A[1] is A[n] , and the right neighbour of A[n] is A[1].

Now your job is to calculate the max sum of a Max-K-sub-sequence. Max-K-sub-sequence means a continuous non-empty sub-sequence which length not exceed K.
 
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases.


Then T lines follow, each line starts with two integers N , K(1<=N<=100000 , 1<=K<=N), then N integers followed(all the integers are between -1000 and 1000).
 
Output
For each test case, you should output a 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 minimum
start position, if still more than one , output the minimum length of them.
 
Sample Input
4
6 3
6 -1 2 -6 5 -5
6 4
6 -1 2 -6 5 -5
6 3
-1 2 -6 5 -5 6
6 6
-1 -1 -1 -1 -1 -1
 
Sample Output
7 1 3
7 1 3
7 6 2
-1 1 1
 
Author
shǎ崽@HDU

求长度不超过k的最大连续子序列。

维护前缀和,把前缀和增加单调队列,对于每个下标,查找单调队列里的最小值,然后做差就能够得到以这个下标结尾的最优解。

代码:

/* ***********************************************
Author :_rabbit
Created Time :2014/5/13 2:06:57
File Name :C.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 1LL<<60
#define eps 1e-8
#define pi acos(-1.0)
typedef __int64 ll;
ll a[201000],sum[200100],que[200100];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ll m,n,T;
cin>>T;
while(T--){//维护前缀和。 scanf("%I64d%I64d",&n,&m);
for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]),a[i+n]=a[i];
sum[0]=0;for(ll i=1;i<=2*n;i++)sum[i]=sum[i-1]+a[i];
ll ans=-INF,start,end;
ll head=0,tail=0,p;que[tail++]=0;
for(ll i=1;i<=2*n;i++){
p=max(0LL,i-m);
while(que[head]<p&&head<tail)head++;//弹出距离i大于m的点。 if(sum[i]-sum[que[head]]>ans){//对于以i结尾的全部序列中,找单调队列中最小的一个元素做差,这样就能够得到以这个元素为结尾的最大和。
ans=sum[i]-sum[que[head]];
start=que[head]+1;end=i;
}
while(head<tail&&sum[que[tail-1]]>sum[i])tail--;//维护一个递增的单调队列。 que[tail++]=i;
}
if(start>n)start-=n;
if(end>n)end-=n;
printf("%I64d %I64d %I64d\n",ans,start,end);
}
return 0;
}

hdu 3415 单调队列的更多相关文章

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

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

  2. HDU 3507 单调队列 斜率优化

    斜率优化的模板题 给出n个数以及M,你可以将这些数划分成几个区间,每个区间的值是里面数的和的平方+M,问所有区间值总和最小是多少. 如果不考虑平方,那么我们显然可以使用队列维护单调性,优化DP的线性方 ...

  3. hdu 3530 单调队列最值

    /** HDU 3530 单调队列的应用 题意: 给定一段序列,求出最长的一段子序列使得该子序列中最大最小只差x满足m<=x<=k. 解题思路: 建立两个单调队列分别递增和递减维护(头尾删 ...

  4. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  5. hdu 3401 单调队列优化+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  6. HDU 2191 - 单调队列优化多重背包

    题目: 传送门呀传送门~ Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种 ...

  7. HDU 3530 单调队列

    题目大意:给你n个数, 让你问你最长的满足要求的区间有多长,区间要求:MAX - MIN >= m && MAX - MIN <= k 思路:单调队列维护递增和递减,在加入 ...

  8. HDU 4122 单调队列

    转载自:http://blog.csdn.net/lvshubao1314/article/details/46910271 DES :给出n个订单和m是商店的开放时间.然后n行给出n个订单的信息.然 ...

  9. HDU 3530Subsequence(单调队列)

    题意 题目链接 给出$n$个数,找出最长的区间,使得区间中最大数$-$最小数 $>= m$ 且$<= k$ Sol 考虑维护两个单调队列. 一个维护$1 - i$的最大值,一个维护$1 - ...

随机推荐

  1. hdu 2078(DFS)

    Matrix Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3845   Accepted: 1993 Descriptio ...

  2. linux grep 查找文件内容

    自试: wang@wang:~$ grep -i "*args*" ~/IGV01-SW/src/bzrobot_diagnostics/bzrobot_lightbelt_man ...

  3. luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  4. Feign详细使用-Spring Cloud学习第四天(非原创)

    文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章   一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...

  5. 布斯(Steve Jobs)在斯坦福大学的演讲稿,中英文对照版

    2005年6月14日,苹果CEO史蒂夫·乔布斯(Steve Jobs)在他的母校斯坦福大学的毕业典礼发表了著名的演讲,关于这段演讲,你会看到N多人的推荐(比如同样喜欢在大学演讲的李开复先生).此前曾经 ...

  6. Leetcode总结之Graph

    package Graph; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections ...

  7. Java EE官方文档汇总

    Java EE是一个开发规范标准,各个容器厂商根据标准去实现,比如Tomcat等,其中Oracle通过标准用GlassFish去实现. 5:https://docs.oracle.com/javaee ...

  8. 【spring mvc】后台的API,测试中,总提示接口实体的某一个字段不能为null,但是明明给值了还提示不能为空

    实体是这三个字段 接口的实现类Controller 前台测试给值 依旧报错 解决方法: 需要添加@RequestBody注解

  9. ※版本管理※=>☆SVN工具=>※解决地域麻烦※№→搭建自己的网络SVN (SourceForge 免费) [转]

    源文 http://blog.csdn.net/xiaoting451292510/article/details/8562570 分类: 版本管理 2013-02-01 14:44 26057人阅读 ...

  10. margin: 0 auto; 元素水平居中布局无效

    失效原因: 要给居中的元素一个宽度,否则无效. 该元素一定不能浮动或绝对定位,否则无效. 在HTML中使用<center></center>标签,需考虑好整体构架,否者全部元素 ...