码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)

时间:2015-04-14 19:49:36      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:单调队列

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
 

Author
shǎ崽@HDU

题意:一个长度为n的圆形序列,求序列长度不超过k的最大值及起点和终点

思路:因为是圆形,所以加倍,然后枚举i,那么单调队列维护i前面k个中的sum最小值 因为sum[i~j]   =sum[j]-sum[i-1]


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 200005

int a[N],sum[N],tail,head,que[N];
int n,k;
int s,e,ma;

inline void inque(int i)
{
     while(head<tail&&sum[i]<=sum[que[tail-1]])
			tail--;
	 que[tail++]=i;
}

inline void outque(int i)
{
	if(i-que[head]>k) head++; //对于i来说,长度为k的第一位应该是 i-k
}

int main()
{
	int i,j,t;
	sf(t);
	while(t--)
	{
		sff(n,k);
		fre(i,1,n+1)
		{
			sf(a[i]);
			sum[i]=sum[i-1]+a[i];
		}
        fre(i,n+1,n*2)
        {
        	sum[i]=sum[i-1]+a[i-n];
        }

       ma=a[1];
       s=e=1;
       tail=head=0;
       inque(0);

       fre(i,1,n*2)
       {
       	  outque(i);
       	  if(ma<sum[i]-sum[que[head]])
		  {
		  	ma=sum[i]-sum[que[head]];
		  	s=que[head]+1; //减去的是sum[que[head]],那么序列的起点应该是que[head]+1
		  	e=i;
		  }
       	  inque(i);
       }

       if(s>n) s-=n;
       if(e>n) e-=n;

       pf("%d %d %d\n",ma,s,e);

	}
	return 0;
}







HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)

标签:单调队列

原文地址:http://blog.csdn.net/u014737310/article/details/45046061

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!