1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| const int maxn = 100000 + 10; int S[maxn], st[maxn]; char str[maxn];
int n, L;
void init() { Set(S, 0); Set(st, 0); Set(str, 0); }
void initCal() { S[0] = 0; _rep(i, 1, n) S[i] = S[i - 1] + str[i] - '0'; }
int minusSlope(int x1, int x2, int x3, int x4) { return (S[x2] - S[x1 - 1]) * (x4 - x3 + 1) - (S[x4] -S[x3 - 1]) * (x2 - x1 + 1); }
int main() { freopen("input.txt", "r", stdin); int kase; scanf("%d", &kase);
while(kase--) { init(); scanf("%d%d%s", &n, &L, str + 1);
// input finished initCal();
int ansL = 1, ansR = n; int l = 0, r = 0; _rep(t, L, n) { // st[l, r) is candidate start point // t - L + 1 represent start point // stack st, put value t - L + 1 while (r - l > 1 && minusSlope(st[r - 2], t - L, st[r - 1], t - L) >= 0) r--; st[r++] = t - L + 1;
// find tangent point st[l] // [st[l], t) is the max while(r - l > 1 && minusSlope(st[l], t, st[l + 1], t) <= 0) l++;
int dslp = minusSlope(st[l], t, ansL, ansR); if(dslp > 0 || (dslp == 0 && t - st[l] < ansR - ansL)) { ansL = st[l]; ansR = t; } } printf("%d %d\n", ansL, ansR); } }
|