# Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers n (1 ≤ n ≤ 100, 000) and L (1 ≤ L ≤ 1, 000) which are the length of a binary sequence and a length lower bound, respectively. In the next line, a string, binary sequence, of length n is given.

# Output

Your program is to write to standard output. Print the starting and ending index of the subsequence.

# Sample Input

2
17 5
00101011011011010
20 4
11100111100111110000

# Sample Output

7 11
6 9

# Solution

#include <iostream>
#include <string>
using namespace std;

const int maxn = 100005;
int n, L, start, ending,temp;
double maxd;
int DNA[maxn],cav[maxn];
string str;

inline int cntAverage(int L, int r, int LL, int rr, int DNA[]){
return (DNA[r]-DNA[L])*(rr-LL) - (DNA[rr]-DNA[LL])*(r-L);
}

inline void changePoint(int pits,int bump){
double temp;
temp = cntAverage(pits, bump, start-1, ending, DNA);
if (temp < 0) return;
if (temp || (bump-pits) < (ending-start+1)){
maxd = temp;
start = pits + 1;
ending = bump;
}
}

int main(){
ios::sync_with_stdio(false);
cin.tie(0);

int cas;
cin >> cas;
while (cas--){
int find = 0, cor = -1;
cin >> n >> L; cin.get();
getline(cin, str);
for (int i = 1; i <= n; ++i)
DNA[i] = DNA[i-1] + (str[i-1] == '1');
maxd = DNA[L] / L, start = 1, ending = L;
for (int i = L; i <= n; ++i){
temp = i - L;
while (find < cor && cntAverage(cav[cor], temp, cav[cor - 1], cav[cor], DNA) <= 0)
--cor;
cav[++cor] = temp;
while (find < cor && cntAverage(cav[find], i, cav[find + 1], i, DNA) <= 0)
++find;
changePoint(cav[find],i);
}
cout << start << ' ' << ending << '\n';
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：UVa OJ 1451 - Average 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)