Max Sum of Max-K-sub-sequence queue monotonic (with tips)

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
***************************************************************************************************************************
Monotonous queue (skillful)
***************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define  inf  0x7fffffff
 8 using namespace std;
 9 int a[100011],sum[201001];
10 int que[201001];
11 int cas,n,k;
12 int st,en,result;
13 void solve()
14 {
15      int head = . 1 , tail = 0 ;
 16      Result = - INF;
 . 17      ST = INF;
 18 is      int IT;
 . 19      for (IT = . 1 ; IT <= n-+ K; IT ++ )
 20 is      {
 21 is          the while (head <= tail && SUM [IT- . 1 ] <SUM [que [tail]]) // condition is not satisfied, the pop-up from the tail 
22 is           tail-- ;
 23 is          the while (head <= tail && que [head] <IT-K) // If out of range, from pop top 
24           head ++ ;
 25          tail ++ ;
26 is          que [tail] = IT- . 1 ; // has value when the pressure here tips 
27          IF (SUM [IT] -sum [que [head]]> Result) // each value updating Excellent 
28          {
 29              Result = SUM [IT] - SUM [que [head]];
 30              ST = que [head] + . 1 ;
 31 is              EN = IT;
 32          }
 33 is      }
 34 is      IF (EN> n-)
 35       en- = n-;
 36  }
 37 [  int main ()
 38 is  {
 39      int I, J;
 40     scanf("%d",&cas);
41     while(cas--)
42     {
43         sum[0]=0;
44         scanf("%d %d",&n,&k);
45         for(i=1;i<=n;i++)
46         {
47             scanf("%d",&a[i]);
48             sum[i]=sum[i-1]+a[i];
49         }
50         for(i=n+1;i<=n+k;i++)
51          sum[i]=sum[i-1]+a[i-n];
52         solve();
53         printf("%d %d %d\n",result,st,en);
54 
55     }
56 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3450678.html

Guess you like

Origin blog.csdn.net/weixin_34357267/article/details/93432859