求y/x的最大,其实就是求斜率最大
#include#include #include #include using namespace std;const int maxn=100000+10;char ss[maxn];int sum[maxn],q[maxn];int n,m,t;int solve(int x1,int x2,int x3,int x4){ return (sum[x2]-sum[x1])*(x4-x3)-(sum[x4]-sum[x3])*(x2-x1);}int main(){ scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); getchar(); scanf("%s",ss+1); getchar(); sum[0]=0; for(int i=1;i<=n;i++) sum[i]=sum[i-1]+ss[i]-'0';//求前缀和,也就是y int i=0,j=0; int ll=0,rr=m; for(int l=m;l<=n;l++)//以l结尾的点 { while(i 0) j--;//维护前端,使其出现凹形 q[j++]=l-m; while(i <=0) i++;//找到以l为结尾的凹形的切线,前面的点都不要 int mm=solve(ll,rr,q[i],l); if(mm<0||m==0&&(l-q[i]