hdu3415 (monotone queue)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3415

Test instructions: A number ring with a length of n containing positive and negative integers, that is, the left side of the 1th is nth. From the selection of a sequence of not more than K, so that the sequence and maximum, the maximum value of the same selected starting point of the smallest, starting point the same length of the smallest.

Analysis: The monotone queue maintenance in the K number of the minimum value of the subscript, and then sweep it all the way, as long as understand the monotonous queue this is water problem.

#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 200010using namespacestd;intA[n],que[n],sum[n],l,r;intSolveintNintk) {     for(intI=1; i<=n;i++) sum[i]=sum[i-1]+A[i]; inthead,tail,mx=-1000000000; Head=tail=0; Que[head]=0;  for(intI=1; i<=n;i++)    {         while(head<=tail&&i-que[head]>k) head++; intj=Que[head]; if(sum[i]-sum[j]>mx) {mx=sum[i]-Sum[j]; L=j+1; r=i; }         while(Head<=tail&&sum[i]<sum[que[tail]]) tail--; que[++tail]=i; } l=l>n/2? l-n/2: l; R=r>n/2? r-n/2: R; returnMX;}intMain () {intt,n,k; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&k);  for(intI=1; i<=n;i++) scanf ("%d", &a[i]), a[i+n]=A[i]; intMx=solve (2*n,k); printf ("%d%d%d\n", Mx,l,r); }}
View Code

hdu3415 (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.