hdu 3415 단조 대기 열 구간 최대 화

http://acm.hdu.edu.cn/showproblem.php?pid=3415
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

 
/**
hdu3415     
    :     N   (N<=10^5)     ,              。              K。
  :        ,            k-1   。   s[i]            i    ,         [i..j]     s[j]-s[i-1]。
     j, s[j]       s[i](i>=j-k)      j        k        。              ,           。
                 (   )       ,       ,       。           ,               ,
          j,   O(1)        s[i]。(              ,            )。
    :    j,    s[j-1]   ,        。          ,           ,                  s[j-1] ,
             。                   ,                  ,                 ,
        。     ,   (i>=j-k) i    s[i]。      ,     ,        (i<j-k)       ,         。(                  )。
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

const int inf=1e9;
const int N=200002;
int n,k,T,a[N],q[N];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        a[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            a[i]+=a[i-1];
        }
        for(int i=n+1;i<n+k;i++)
            a[i]=a[n]+a[i-n];
        int m=n+k-1;
        int head=0,tail=0;
        int maxx=-inf;
        int l,r;
        for(int i=1;i<=m;i++)
        {
            while(head<tail&&a[i-1]<a[q[tail-1]])
                tail--;
            q[tail++]=i-1;
            while(head<tail&&i-q[head]>k)
                  head++;
            if(maxx<a[i]-a[q[head]])
            {
                maxx=a[i]-a[q[head]];
                l=q[head]+1;
                r=i>n?i%n:i;
            }
        }
        printf("%d %d %d
",maxx,l,r); } return 0; }

좋은 웹페이지 즐겨찾기