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)