题目地址:http://vjudge.net/problem/UVA-1451
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=(int)(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(int)(b);--i)
const int maxn=100000+5;
char s[maxn];
int sum[maxn],Q[maxn];
int cmp(int x1,int x2,int x3,int x4){ //比较斜率return (sum[x2]-sum[x1-1])*(x4-x3+1) - (sum[x4]-sum[x3-1])*(x2-x1+1);
}
int main(int argc, char const *argv[])
{int T,n,L; scanf("%d",&T);while(T--&&scanf("%d%d%s",&n,&L,s+1)){REP(i,1,n) sum[i]=sum[i-1]+(s[i]=='1');int ansL=1,ansR=L;int i=0,j=0; //front,rearREP(t,L,n){while(j-i>1&&cmp(Q[j-2],t-L,Q[j-1],t-L)>=0) j--;Q[j++]=t-L+1;while(j-i>1&&cmp(Q[i],t,Q[i+1],t)<=0) i++;int c=cmp(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;
}