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)