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. Android系统默认语言改为中文

    第一种方法: 修改 build/tools/buildinfo.sh echo "ro.product.locale.language=zh"echo "ro.produ ...

  2. Linux 之 LNMP服务器搭建-Nginx

    LNMP服务器搭建-Nginx 参考教程:[千峰教育] 系统版本: CentOS 6.8 关闭防火墙和Selinux service iptables stop setenforce 安装Nginx ...

  3. dedecms--后台添加会员栏目(批量添加)

    最近在用dedecms二次开发会员功能,一开始做了一个会员添加,但是领导要求可以批量添加,最好是可以输入添加个数:这样我想添加几个就添加几个了 1:会员添加的htm页面 <html> &l ...

  4. Arduino可穿戴教程ArduinoIDE新建编辑源文件

    Arduino可穿戴教程ArduinoIDE新建编辑源文件 Arduino IDE新建源文件 Arduino IDE启动后默认就新建了一个源文件,如图2.20所示.新建的源文件名称是以sketch_开 ...

  5. 网络数据注入工具HexInject

    网络数据注入工具HexInject   对于Kali Linux提供的工具HexInject来说,数据注入才是其最重要的功能.它可以直接向网络注入渗透人员构造的数据包,也可以篡改网络传输的数据.为了避 ...

  6. Leetcode总结之Tree

    package Tree; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; imp ...

  7. Eclipse安装Spring工具套件

    前言: 安装spring工具套件是为了更快捷的使用spring,但是我觉得既然已经有了maven,工具套件其实不那么重要. 而且装好后我发觉没什么两样,只是新建bean文件时比较爽一点. 安装步骤: ...

  8. 修改ViewPager调用setCurrentItem时,滑屏的速度 ,解决滑动之间切换动画难看

    在使用ViewPager的过程中,有需要直接跳转到某一个页面的情况,这个时候就需要用到ViewPager的setCurrentItem方法了,它的意思是跳转到ViewPager的指定页面,但在使用这个 ...

  9. Direct2D教程(一)Direct2D已经来了,谁是GDI的终结者?

    什么是Direct2D 一言以蔽之,就是Windows 7平台上的一个2D图形API,可以提供高性能,高质量的2D渲染.大多数人对Direct2D可能都比较陌生,以至于我之前在论坛上提到这个词的时候, ...

  10. JS里面的call, apply以及bind

    参考了这篇文章:http://www.tuicool.com/articles/EVF3Eb 给几个例子 function add(a,b) { alert(a+b); } function sub( ...