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

来源:互联网 发布:淘宝开店手机端 编辑:程序博客网 时间:2024/06/05 14:48



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


很无语,就是不知道怎么错了,wr了几遍,决定以后还是用STL库吧,数组老是不知道哪出个错误,如果用库函数的话,注意要用deque,因为头也得去掉

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<math.h>using namespace std;#define ll long longconst int INF=0x7fffffff;const int N=100000+2;int v[N],q[2*N],sum[2*N],n,k;int main(){       //若表示区间[a,b],用sum[b]-sum[a-1]    int t;    scanf("%d",&t);    while (t--)    {        int i;        memset(q,0,sizeof(0));        sum[0]=0;        scanf("%d%d",&n,&k);        for (i=1;i<=n;i++)        {              scanf("%d",&v[i]);              sum[i]=sum[i-1]+v[i];        }          for (i=n+1;i<n+k;i++)            sum[i]=sum[i-1]+v[i-n];          int s=0,e=0,ans=-INF;          int l=0,r=-1;          for (i=1;i<n+k;i++)          {              while (l<=r&&sum[i-1]<sum[q[r]])                r--;                q[++r]=i-1;     //注意这里哈,就是这wr了几遍,敲习惯了,直接按着模板上了              while (l<=r&&q[l]<i-k)                l++;              if (sum[i]-sum[q[l]]>ans)                {                    ans=sum[i]-sum[q[l]];                    s=q[l]+1;   //注意这里                    e=i;                }          }          if (e>n)            e-=n;          printf("%d %d %d\n",ans,s,e);    }    return 0;}


0 0