HDU 3415 Max Sum of max-k-sub-sequence (monotone queue)

Source: Internet
Author: User

Title Link: Hdu 3415 Max Sum of Max-k-sub-sequence

Test instructions

Give you a bunch of rings to form the number, so you find a length of not greater than the sub-section of K make and maximum.

Exercises

We first put the head and tail together, so that the number of the former I and Sum[i].

Then the question becomes a max{sum[i]-sum[j]} (I-k<j<i)

For each sum[i], we just need to find a minimum of sum[j] that satisfies the condition, and then we can use a monotone queue to maintain it.

1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i=a;i<=b;i++)3 using namespacestd;4 5 Const intn=1e5+7, inf=1<< -;6 7 intt,n,k,sum[2*n],a[n],q[2*N],ans,l,r;8 9 voidUpintIintj) {if(Sum[i]-sum[j]>ans) ans=sum[i]-sum[j],l=j+1, r=i;}Ten  One intMain () A { -scanf"%d",&t); -      while(t--) the     { -scanf"%d%d",&n,&k); -F (I,1, N) scanf ("%d", A +i); -F (I,1, N) sum[i]=sum[i-1]+A[i]; +F (i,n+1,2*n) sum[i]=sum[i-1]+a[i-n]; -         intHead=1, tail=0; +q[++tail]=0, Ans=-inf;//Initialize AF (I,1,2*N) at         { -              while(head<=tail&& (I-q[head]) >k) head++; - Up (I,q[head]); -              while(Head<=tail&&sum[q[tail]]>=sum[i]) tail--; -q[++tail]=i; -         } inr%=N; -printf"%d%d%d\n", ans,l,r==0?n:r); to     } +     return 0; -}
View Code

HDU 3415 Max Sum of max-k-sub-sequence (monotone queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.