hdu 3415 Max Sum of Max-K-sub-sequence

来源:互联网 发布:下载excel办公软件 编辑:程序博客网 时间:2024/05/20 21:20

题目描述:

Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4208    Accepted Submission(s): 1503


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
46 36 -1 2 -6 5 -56 46 -1 2 -6 5 -56 3-1 2 -6 5 -5 66 6-1 -1 -1 -1 -1 -1
 

Sample Output
7 1 37 1 37 6 2-1 1 1
 

Author
shǎ崽@HDU
 

Source
HDOJ Monthly Contest – 2010.06.05
 

Recommend
lcy

思路:
估计是dp
状态转移: dp[i]=sum[i]-min(sum[j])     (i-k<j<i)
维护sum[i] 最小就行
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define inf 0xfffffff#define BUG puts("Here");system("pause")using namespace std;int const nMax =200010;typedef long long LL;LL dp[nMax];LL sum[nMax];int l[nMax];int r[nMax];LL a[nMax];int n,k;int q[nMax],he,ta;void queue(int i){    while(he<ta){        int j=q[he],jj=q[he+1];        if(i-j>k||sum[jj]<sum[j])he++;        else break;    }    dp[i]=sum[i]-sum[q[he]];    l[i]=q[he]+1;    return ;}void insert(int i){    while(he<ta){        int j=q[ta];        if(sum[i]<sum[j])ta--;        else break;    }    q[++ta]=i;    return ;}void DP(){    for(int i=1;i<=n+k;i++){        queue(i);        insert(i);     //   printf("dp[%d]=%d\n",i,dp[i]);    }    LL ans=-inf; //printf("ans=%d\n",ans);    int al=0,ar(0);    for(int i=1;i<=n+k;i++)if(dp[i]>ans){        ans=dp[i];// printf("  --dp[%d]=%d ans=%d\n",i,dp[i],ans);        al=l[i];        ar=i;    }else if(ans==dp[i]){        if(l[i]<al){            ar=i;            al=l[i];        }else        if(l[i]==al&&(ar-al>i-l[i])){            ar=i;           al=l[i];       }    }    if(al>n)al-=n;    if(ar>n)ar-=n;    printf("%I64d %d %d\n",ans,al,ar);    return ;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        sum[0]=0;        dp[0]=0;        scanf("%d%d",&n,&k);        for(int i=1;i<=n;i++){            scanf("%I64d",&a[i]);            sum[i]=sum[i-1]+a[i];        }        for(int i=n+1;i<=2*n;i++){            sum[i]=sum[i-1]+a[i-n];        }       // BUG;        ta=he=0;        q[0]=0;        DP();    }    return 0;}