Max Sum of Max-K-sub-sequence

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

Appoint description: 
System Crawler  (2016-07-10)

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
 
 
题意:
一个环,求长度小于等于k的连续子串值最大。
思路:
对于环,一般的处理就是在后面重复添加一段,求前缀和。然后维护一个递增的队列,如果当前的值比队尾的值小,说明对于后面的数来说,与当前位置sum的差肯定大于队尾的数,所以删除队尾的数,直到队尾的数小于当前的值或者队列空。因为队列长度不能超过k,所以从队头开始删,直到队头的数的位置大于等于当前的位置-k。当前的最值就是当前位置的值减去队头的值,然后维护总的值即可。
 
/*
* Author: sweat123
* Created Time: 2016/7/11 21:46:18
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
deque<int>q;
int a[MAXN],n,k,sum[MAXN],cnt;
int main(){
int t;
scanf("%d",&t);
while(t--){
q.clear();
scanf("%d%d",&n,&k);
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
for(int i = ; i < k; i++){
a[i+n] = a[i];
}
sum[] = ;
for(int i = ; i < n + k; i++){
sum[i] = sum[i-] + a[i];
}
int ans = -INF,l,r;
for(int i = ; i < n + k; i++){
while(!q.empty() && sum[q.back()] > sum[i-]){
q.pop_back();
}
while(!q.empty() && q.front() < (i - k)){
q.pop_front();
}
q.push_back(i-);
int val = sum[i] - sum[q.front()];
if(val > ans){
ans = val;
l = q.front();
r = i;
}
}
if(r > n) r %= n;
printf("%d %d %d\n",ans,l+,r);
}
return ;
}
 
 

hdu3415 单调队列的更多相关文章

  1. hdu3415 单调队列模板题

    比较裸的单调队列 先求前缀和,枚举所有结束位置1~n+k即可 #include<iostream> #include<cstdio> #include<cstring&g ...

  2. hdu3415单调队列

    题意:       给你一个数字组成的环,要求在里面找到一个最大的子序列,使得和最大,要求: (1)子序列长度不能超过k (2)如果子序列和相同要起点最小的 (3)如果起点相同要长度最小的 思路:   ...

  3. hdu3415 Max Sum of Max-K-sub-sequence 单调队列

    //hdu3415 Max Sum of Max-K-sub-sequence //单调队列 //首先想到了预处理出前缀和利用s[i] - s[j]表示(j,i]段的和 //之后的问题就转换成了求一个 ...

  4. poj2823/hdu3415 - 数据结构 单调队列

    poj2823 题目链接 长度为N的数组,求宽度k的滑动窗口在数组上滑动时窗口内的最大值或最小值 如果用单调队列做,求最小值时,队列应该严格递增的.所以插入时,队尾大于等于插入值的元素都应被舍弃,因为 ...

  5. hdu3415:最大k子段和,单调队列

    题目大意:给定长度为n的数组,求出最大的区间和,其中区间长度在[1,k]之间 分析: 学动态规划的时候我们会遇到一个经典问题 最大子段和,这个题跟最大子段和很类似 不同的是区间的长度有限制,无法用原算 ...

  6. HDU3415:Max Sum of Max-K-sub-sequence(单调队列)

    Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left ...

  7. hdu3415(单调队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 题意:一个长度为n包含正负整数的数环,即第1个的左边是第n个.从中选一个不超过k的序列,使得序列 ...

  8. HDU3415【单调队列】

    单调队列解决通过维护满足条件内的值,并保证队列里的值单调,解决一个最大最小. 让你求一个k区间长度的最大值,那么就只要搞下前缀和, sum[ i , j ] 区间的和:sum[ j ]-sum[ i ...

  9. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

随机推荐

  1. SJPullDownMenu下拉菜单框架使用

    SJPullDownMenu 快速集成类似淘宝筛选下拉菜单 如果页面显示不全等问题请转至:http://www.jianshu.com/p/d07c6393830c 查看使用 Getting Star ...

  2. cocos2dx骨骼动画Armature源码分析(三)

    代码目录结构 cocos2dx里骨骼动画代码在cocos -> editor-support -> cocostudio文件夹中,win下通过筛选器,文件结构如下.(mac下没有分,是整个 ...

  3. iOS开发 适配iOS10以及Xcode8[转]

    现在在苹果的官网上,我们已经可以下载到Xcode8的GM版本了,加上9.14日凌晨,苹果就要正式推出iOS10系统的推送了,在此之际,iOS10的适配已经迫在眉睫啦,不知道Xcode8 beat版本, ...

  4. shell

    查看本机的shell有哪些 cat /etc/shells切换shell(zsh) chsh -s /bin/zsh 切换默认shell(bash) chsh -s /bin/bash  

  5. yii2下拉框带搜索功能

    简单的小功能,但是用起来还是蛮爽的.分享出来让更多的人有更快的开发效率,开开心心快乐编程.作者:白狼 出处:http://www.manks.top/yii2_dropdown_search.html ...

  6. Ajax.BeginForm参数详解

    在Asp.Net的MVC中的语法,在Razor页面中使用,替代JQuery的Ajax使用,方便快捷. 使用Ajax.BeginForm方法会生成一个form表单,最后以Ajax的方式提交表单数据:需要 ...

  7. ORA-00600: internal error code, arguments: [LibraryCacheNotEmptyOnClose]

      案例环境: 操作系统版本: Red Hat Enterprise Linux ES release 4 数据库版本  : 10.2.0.4.0 32 bit 案例介绍: 今天我执行stop_ora ...

  8. Android资源(图片)命名规范

    (转自:http://www.jb51.net/article/38796.htm) 图片命名注意: 1,不能以下划线("_")开头: 2,以数字加下划线("[0-9]_ ...

  9. Redis学习笔记2-Redis的安装体验

    Redis的官方只提供了Linux版本的,并没提供Windows版本的(不过非官方有windows版本的.可以下载下来做开发测试学习用非常方便.博客后面会介绍到的).Linux下安装过程如下[以下命令 ...

  10. 透明ActionBar

    代码方式:    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceSta ...