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
 

这是我们集训比赛的一道题,出题人说是什么DP,坑爹啊,比赛完后一看,单调队列,DP还做不出来

然后就果断看了一下单调队列,之是参考别人的代码后才写出来的

单调队列即保持队列中的元素单调递增(或递减)的这样一个队列,可以从两头删除,只能从队尾插入。单调队列的具体作用在于,由于保持队列中的元素满足单调性,对于上述问题中的每个j,可以用O(1)的时间找到对应的s[i]。(保持队列中的元素单调增的话,队首元素便是所要的元素了)。

维护方法:对于每个j,我们插入s[j-1](为什么不是s[j]? 队列里面维护的是区间开始的下标,j是区间结束的下标),插入时从队尾插入。为了保证队列的单调性,我们从队尾开始删除元素,直到队尾元素比当前需要插入的元素优(本题中是值比待插入元素小,位置比待插入元素靠前,不过后面这一个条件可以不考虑),就将当前元素插入到队尾。之所以可以将之前的队列尾部元素全部删除,是因为它们已经不可能成为最优的元素了,因为当前要插入的元素位置比它们靠前,值比它们小。我们要找的,是满足(i>=j-k+1)的i中最小的s[i],位置越大越可能成为后面的j的最优s[i]。

在插入元素后,从队首开始,将不符合限制条件(i>=j-k+1)的元素全部删除,此时队列一定不为空。(因为刚刚插入了一个一定符合条件的元素)

#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std; int a[111111];
int sum[211111];
const int INF = 0x3fffffff; int main()
{
int t,n,m,i,j,k,head,end;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
j = n;
sum[0] = 0;
for(i = 1; i<=n; i++)
{
scanf("%d",&a[i]);
sum[i] = sum[i-1]+a[i];//将前i项和全部存入sum数组中
}
int ans = -INF;
for(i = n+1; i<n+k;i++)
sum[i] = sum[i-1]+a[i-n];
n = n+k-1;
deque<int> Q;//双向队列
Q.clear();
for(i = 1; i<=n; i++)
{
while(!Q.empty() && sum[i-1]<sum[Q.back()])//保持队列的单调性
Q.pop_back();
while(!Q.empty() && Q.front()<i-k)//超过k的长度则消除队列前面的元素
Q.pop_front();
Q.push_back(i-1);
if(sum[i]-sum[Q.front()]>ans)//记录,sum[n]-sum[m]所得出的是n-1到m+1之间的和
{
ans = sum[i]-sum[Q.front()];
head = Q.front()+1;
end = i;
}
}
if(end>j)
end%=j;
printf("%d %d %d\n",ans,head,end);
} return 0;
}

HDU3415:Max Sum of Max-K-sub-sequence(单调队列)的更多相关文章

  1. poj3017 Cut the Sequence 单调队列 + 堆 dp

    描述 把一个正数列 $A$分成若干段, 每段之和 不超过 $M$, 并且使得每段数列的最大值的和最小, 求出这个最小值. 题目链接 题解 首先我们可以列出一个$O(n^2)$ 的转移方程 : $F_i ...

  2. $Poj3017\ Cut\ The\ Sequence$ 单调队列优化$DP$

    Poj   AcWing Description 给定一个长度为N的序列 A,要求把该序列分成若干段,在满足“每段中所有数的和”不超过M的前提下,让“每段中所有数的最大值”之和最小. N<=10 ...

  3. POJ 3709 K-Anonymous Sequence (单调队列优化)

    题意:给定一个不下降数列,一个K,将数列分成若干段,每段的数字个数不小于K,每段的代价是这段内每个数字减去这段中最小数字之和.求一种分法使得总代价最小? 思路:F[i]表示到i的最小代价.f[i]=m ...

  4. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  5. hdu3415 Max Sum of Max-K-sub-sequence

       Max Sum of Max-K-sub-sequence Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64 ...

  6. Max Sum of Max-K-sub-sequence hdu3415

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

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

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

  8. K - Max Sum Plus Plus

    K - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

随机推荐

  1. hdu 4676 Sum Of Gcd 莫队+数论

    题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...

  2. 给进程分配cpu核心

    新负责的程序采用生产者和消费者的模式,生产者的速度非常快,数据几乎都在内存里,处理起来很快.而消费者要频繁的I/O.所以打算给生产者和消费者分配不一样的核心. 生产者只需要一个核心就够了,其余分配给消 ...

  3. 利用VHDL读写file文件

    library ieee;     use std.textio.all;     use ieee.std_logic_textio.all;     use ieee.std_logic_1164 ...

  4. [问题解决] 程序部署到Linux服务器乱码

    错误: 在windows下开发的eclipse项目需要用java mail发送邮件,在将整个项目部署到linux服务器之后发送的邮件出现了乱码. 发生场景: Linux服务器下的Java mail程序 ...

  5. image.xx.com 通过haproxy 跳转到内部图片服务器

    <pre name="code" class="html">http://www.hyyche.com/#main C:\Users\Adminis ...

  6. Java中文乱码问题研究(二)

    上面写了console的乱码问题,接下来写的是web中servlet中的问题,大楷我比较关心一点,因为遇到这个的情况多一些吧.直接开始吧. 2. jsp和servlet中的乱码问题 分析: a. 其实 ...

  7. Radio Link Failure and Recovery

    四种会发生Radio Link Failure的场景 -  DL Physical Layer Failure (PDCCH BLER > 10%) -  Random Access Probl ...

  8. image元素的src属性值与getAttribute('src')值

    采集的时候,当采集到一些不可用的照片就将其剔除掉 我的解决思路是new一个img对象, 然后将采集过来的图片赋值给这个img, 然后分别处理img的onerror和 onload, 当在onerror ...

  9. 自己定义flash的宽和高

    前段时间做个项目,是个网页的聊天界面,聊天的内容使用flash制作,我需要将flash的swf插件放到页面上,然后获取聊天内容, 1.将文件在页面上显现出来: 如图,正中间使用后台制作出来的swf文件 ...

  10. The file “XXX” couldn’t be opened because you don’t have permission to view it.解决方法:

    The file “XXX” couldn’t be opened because you don’t have permission to view it.解决方法:   解决方法:直接点击Xcod ...