[Monotone queue] Hdu 3415 Max Sum of Max-k-sub-sequence

Source: Internet
Author: User

Test instructions

Give N and K, and give you n the number of rings formed

Ask you the maximum number of consecutive k and how much

And the output interval, and the same as the left end of the smallest, again the smallest length

Ideas:

We record prefixes and sum[i]

Open a monotonic queue maintain sum[i-1] with the lowest value

Because for the sum[i]-sum[j to the current position], the smaller the sum[j], the greater the sum[i]

So the maintenance is to the current position to meet the minimum requirements of the sun[j]

Code:

#include "cstdlib" #include "Cstdio" #include "CString" #include "Cmath" #include "queue" #include "algorithm" #include "    iostream "#include" map "using namespace std; #define N 222222int sum[n],v[n];int q[2*n];int main () {int t;    cin>>t;        while (t--) {int n,k;        scanf ("%d%d", &n,&k);        deque<int>q;        sum[0]=0;            for (int i=1;i<=n;i++) {scanf ("%d", &v[i]);        Sum[i]=sum[i-1]+v[i];        } for (int i=n+1;i<2*n;i++) sum[i]=sum[i-1]+v[i-n];        int ans=-999999999,x,y;        int top=0,ed=0;            for (int i=1;i<2*n;i++) {while (top<ed && sum[q[ed-1]]>=sum[i-1]) ed--;            Q[ed++]=i-1;            while (top<ed && i-q[top]>k) top++;            int Tep=sum[i]-sum[q[top]];                if (Tep>ans) {ans=tep;                x=q[top]+1;            Y=i;        }} if (Y>n) y-=n; printf ("%d%d%d\n", ans, x, y); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Monotone queue] Hdu 3415 Max Sum of Max-k-sub-sequence

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.