HDU 3415 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 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;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值