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下进行域名的解析? /** *  域名解析ip * *  @param hostName 域名 * *  @return ip */ +(NSString *) getIPWithHost ...

  2. [Java编程思想-学习笔记]第1章 对象导论

    1.1  抽象过程 Java是一门面向对象的语言,它的一个优点在于只针对待解问题抽象,而不用为具体的计算机结构而烦心,这使得Java有完美的移植性,也即Java的口号"Write Once, ...

  3. Java中GC的工作原理

    转文: 一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率,才能提高整个 ...

  4. OracleHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

  5. SqlHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

  6. leveldb源码分析--SSTable之Compaction

    对于compaction是leveldb中体量最大的一部分,也应该是最为复杂的部分,为了便于理解我们首先从一些基本的概念开始.下面是一些从doc/impl.html中翻译和整理的内容: Level 0 ...

  7. mysql权限与安全

    一.MySQL权限系统通过两个阶段进行认证: (A) 对用户进行身份认证,IP地址和用户名联合, (B) 对合法用户赋予相应权限,权限表在数据库启动的时候载入内存中. 二.在权限的存取过程中,会用到& ...

  8. 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接(转)

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...

  9. 从零自学Hadoop(07):Eclipse插件

    阅读目录 序 Eclipse Eclipse插件 新建插件项目 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写 ...

  10. C++智能指针详解

    本文出自http://mxdxm.iteye.com/ 一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最 ...