The surface let us ask for a string problem, but can be converted to the problem of slope, Purple book has been said very clearly, I will not repeat here.
The code is as follows:
#include <bits/stdc++.h>using namespace Std;const int maxn = 100000 + 5;int n,t,l;double A[maxn],p[maxn];char S[MAXN ];int campare (int x1,int x2,int x3,int x4) {//linear p[x1]p[x2] slope minus p[x3]p[x4] return (A[X1]-a[x2-1]) * (x3-x4+1)-(a[ X3]-a[x4-1]) * (x1-x2+1); }int Main () {scanf ("%d", &t); while (t--) {scanf ("%d%d%s", &n,&l,s); int len = strlen (s); A[0] = 0; for (int i=0;i<len;i++) {a[i+1] = A[i] + s[i]-' 0 ';//a[i] is the first and} int l = 1, R = L, i = 0, j = 0; Using two pointers I and J to dynamically maintain the lower convex interval. for (int t = l;t <= len;t + +) {while (J-i > 1 && campare (t-l,p[j-2],t-l,p[j-1]) >=0) j--;//delete The convex point p[j++] = t-l + 1; Add new elements while (J-i > 1 && campare (T,p[i+1],t,p[i]) >=0) i++; Determine the new pointcut, because the X coordinate of the tangent point must be an increasing int c = Campare (R,l,t,p[i]); if (C < 0 | | c = = 0 && (r-l) > (T-p[i])) {L = P[i]; r = t; } } cout<<l<< ' <<r<<endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1451-average (combination of several shapes)