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. iOS证书和描述文件

    iOS有两种证书和描述文件: 证书类型 使用场景 开发(Development)证书和描述文件 用于开发测试,在HBuilder中打包后可在真机环境通过Safari调试 发布(Distribution ...

  2. shell脚步传参

    linux系统除了提供位置参数还提供内置参数,内置参数如下: $# ----传递给程序的总的参数数目 $? ----上一个代码或者shell程序在shell中退出的情况,如果正常退出则返回0,反之为非 ...

  3. iOS切图文件的命名规范

    万能公式:

  4. OC 类簇与复合

    OC 类簇与复合 类簇: 类簇是Foundation框架中广泛使用的设计模式.类簇将一些私有的.具体的子类组合在一个公共的.抽象的超类下面,以这种方法来组织类可以简化一个面向对象框架的公开架构,而又不 ...

  5. 开发者调试工具Chrome Workspace

    Workspace是个什么样的东西呢?他能够在开发者工具中调试修改js或者css同时自动保存文件,能够避免开发人员在工具中调试好,再到编辑器中修改一次代码的重复操作,能够提高一定的效率 配置Chrom ...

  6. C# 6.0新特性---语法糖

    转载:http://www.cnblogs.com/TianFang/p/3928172.html 所谓语法糖就是在编译器里写做文章,达到简化代码书写的目的,要慎重使用,省略过多不易理解. NULL检 ...

  7. break、continue、return

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. ORACLE数据库汉字占几个字节问题

    一同事由于系统需求关系,将SQL SERVER数据库的一个表导入ORACLE数据库时,发现居然报错:ORA-12899: value too large for column xxxx (actual ...

  9. Reporting Services 错误案例一则

    遇到一个有意思的Reporting Services报表的案例,在2015-01-30号的凌晨20分左右的时候,有人发现Reporting Services的速度非常慢,而且最后有抛出异常,当时不知道 ...

  10. apache ab测试命令详解

    这篇文章主要介绍了apache性能测试工具ab使用详解,需要的朋友可以参考下   网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环.只有让服务器处在高压情况下,才能真正体现出软件.硬件等各种 ...