https://ac.nowcoder.com/acm/contest/893/F
题意:一串01串,m次修改;使得连续0或1最长,问最长多长;
思路:(1)二分长度判断是否m次能完成;处理前缀和尺取判断区间;
(2) 直接尺取0或1;
#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<map>
#include<stack>
#include<string>
#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn=1e6+9;
const int mod=998244353;
inline ll read()
{
ll f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9')
{
if(ss=='-')f=-1;ss=getchar();
}
while(ss>='0'&&ss<='9')
{
x=x*10+ss-'0';ss=getchar();
} return f*x;
}
int sum0[maxn],sum1[maxn];
char s[maxn];
int n,m;
bool ch(int len)
{
int l=1;
int r=len;
while(r<=n)
{
if(sum0[r]-sum0[l-1]<=m) return 1;
if(sum1[r]-sum1[l-1]<=m) return 1;
l++;
r++;
}
return 0;
}
int main()
{
//FAST_IO;
//freopen("input.txt","r",stdin);
int t;
t=read();
while(t--)
{
mem(sum0,0);
mem(sum1,0);
n=read();
m=read();
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{
if(s[i]=='0')
{
sum0[i]=sum0[i-1]+1;
sum1[i]=sum1[i-1];
}
else
{
sum0[i]=sum0[i-1];
sum1[i]=sum1[i-1]+1;
}
}
int l=0;
int r=n;
int ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(ch(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}
/*
123
6 3
010101
6 2
010101
10 1
0111001000
*/
#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<map>
#include<stack>
#include<string>
#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn=1e6+9;
const int mod=998244353;
inline ll read()
{
ll f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9')
{
if(ss=='-')f=-1;ss=getchar();
}
while(ss>='0'&&ss<='9')
{
x=x*10+ss-'0';ss=getchar();
} return f*x;
}
char s[maxn];
int n,m;
int main()
{
//FAST_IO;
//freopen("input.txt","r",stdin);
int t;
t=read();
while(t--)
{
n=read();
m=read();
scanf("%s",s+1);
int r=1;
int l=1;
int ans=0;
int tmp=m;
while(l<=n)
{
while(r<=n)
{
if(!m&&s[r]=='0') break;
if(s[r]=='0') m--;
r++;
ans=max(ans,r-l);
}
//cout<<l<<" "<<r<<endl;
if(s[l]=='0') m++;
l++;
}
//cout<<ans<<endl;
m=tmp;
r=1;
l=1;
while(l<=n)
{
while(r<=n)
{
if(!m&&s[r]=='1') break;
if(s[r]=='1') m--;
r++;
ans=max(ans,r-l);
}
//cout<<l<<" "<<r<<endl;
if(s[l]=='1') m++;
l++;
}
printf("%d\n",ans);
}
return 0;
}
/*
123
6 3
010101
6 2
010101
10 1
0111001000
*/