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. win7显示不是正版系统的解决方法

    十一长假回来,打开电脑就变成这样了.现在的我已经学会了不再逃避问题,要学着解决问题,就在网上搜集了有关这方面的信息.说是下载一个激活工具就可以了.我就试着下载了,但是不知道为什么下了几个激活工具都不管 ...

  2. Linux0.11内核--加载可执行二进制文件之3.exec

    最后剩下最核心的函数do_execve了,由于这里为了简单起见我不分析shell命令的情况, /* * 'do_execve()'函数执行一个新程序. */ //// execve()系统中断调用函数 ...

  3. dede:field name='imgurls'不能二次使用的解决办法

    {dede:field name='imgurls' alt='图片输出区'}图片链接  [field:linkurl/]图片地址 [field:imgsrc/]{/dede:field} 这个标签不 ...

  4. Android Studio 快捷键一览

    刚从 eclipse 转到 android studio 的同学,编写代码时使用的快捷键不同,一时难以适应,当然可以通过设置,将快捷键模板设置成与 eclipse 相同的,但我个人不赞成,因为 And ...

  5. mysql 基本

    mysql -u root -p 输入密码进入数据库 show database; 查询当前库 use databasename 切换到某个库 show tables; 列出当前数据库的表 desc ...

  6. GitHub for Windows提交失败“failed to sync this branch”

    今天github for windows同步推送远端github出问题了,提交到本地没问题,远端一直推送不上去,挺棘手的,试了几个网上的方法不管用.问题如下,报这个错: failed to sync ...

  7. 作业配置规范文档[MS SQL]

    作业配置规范文档(MS SQL) 文档类型 MS SQL数据库作业配置规范文档 创建日期 2015-07-30 版本变化 V3.0 修改记录 修改人 修改日期 版本 修改描述 潇湘隐者 2015-08 ...

  8. Thrift:Quick Start

    Thrift 快速开始 1 Thrift 介绍 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其中所用到的 ...

  9. 【hive】——metastore的三种模式

    Hive中metastore(元数据存储)的三种方式: 内嵌Derby方式 Local方式 Remote方式 [一].内嵌Derby方式 这个是Hive默认的启动模式,一般用于单元测试,这种存储方式有 ...

  10. 自动创建WIN32下多级子目录的C++封装类

            这是 WIN32 自动创建多级子目录的 C++ 封装类,用法简单.         封装没有采用类的静态函数方式,而是在构造函数里面直接完成工作.没什么具体的原因,只是当时做成这样了, ...