Absrtact: Combination of numbers, slope optimization, monotone queue.
Test instructions: For a length of n 01 strings of the substring, the substring length is at least l, the average should be as large as possible, more than meet the conditions to take the shortest length, there are many words, take the starting point is the most left.
Find the prefix and s[i], so that the point Pi (I,s[i]), then this problem is converted to the maximum slope of the two points. Drawing analysis shows that if there is a convex point, then the upper convex point, it must not be optimal, so the problem becomes the maintenance of a convex curve. It can then be maintained by comparing the slope, which requires a tangent point, which does not get a better solution before the points that were previously calculated.
Assuming at point A, that is, before the tangent, then the point of the previous selection is not optimal, assuming that at point B, the original tangent, then, how can not get a slope than the previous tangent line larger.
More specifically, you can read this paper: a brief discussion on the application of combination of numbers and shapes in the competition of informatics
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5+233;CharS[MAXN];intSUM[MAXN],Q[MAXN];#defineSEG (X1,X2) (Sum[x2]-sum[x1-1])inlineintCmp_ave (intX1,intX2,intX3,intx4) { returnSEG (x1,x2) * (x4-x3+1)-SEG (x3,x4) * (x2-x1+1);}intMain () {//freopen ("In.txt", "R", stdin); intT scanf"%d",&T); while(t--){ intn,l; scanf ("%d%d%s", &n,&l,s+1); sum[0] =0; for(inti =1; I <= N; i++) Sum[i]= sum[i-1] + s[i]-'0'; intAnsl =1, AnsR =L; inti =0, j =0; for(intt = L; t <= N; t++){ while(j-i>1&& Cmp_ave (q[j-2],t-l,q[j-1],T-L) >=0) j--; Q[j+ +] = t-l+1; while(j-i>1&& Cmp_ave (q[i],t,q[i+1],T) <=0) i++; intc =Cmp_ave (Q[I],T,ANSL,ANSR); if(C >0|| c = =0&& T-q[i] < AnsR-ansl) {Ansl= Q[i]; AnsR =T; }} printf ("%d%d\n", ANSL,ANSR); } return 0;}
UVA 1451 Average Seol Average