單調隊列最佳化DP

來源:互聯網
上載者:User

標籤:

Max Sum of Max-K-sub-sequence

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


Problem DescriptionGiven 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. 

 

InputThe 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). 

 

OutputFor 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 Input46 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 Output7 1 37 1 37 6 2-1 1 1 

 

Authorshǎ崽@HDU 

 

SourceHDOJ Monthly Contest – 2010.06.05  
#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<cmath>#include<algorithm>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<set>using namespace std;#define INF 100000000int tt,n,k;int sum[200005],a[200005],hd,ed;int main(){    scanf("%d",&tt);    while(tt--)    {        scanf("%d%d",&n,&k);        int j=n;        sum[0]=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            sum[i]=sum[i-1]+a[i];        }        for(int i=n+1;i<n+k;i++)        {            sum[i]=sum[i-1]+a[i-n];        }        n=n+k-1;        int ans=-INF;        deque<int> q;        q.clear();        for(int i=1;i<=n;i++)        {            while(!q.empty()&&sum[i-1]<sum[q.back()])//維護單調性                q.pop_back();            while(!q.empty()&&q.front()<i-k)                q.pop_front();            q.push_back(i-1);            if(sum[i]-sum[q.front()]>ans)            {                ans=sum[i]-sum[q.front()];                hd=q.front()+1;                ed=i;            }        }        if(ed>j)            ed=ed%j;        printf("%d %d %d\n",ans,hd,ed);    }    return 0;}

  

單調隊列最佳化DP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.