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