4.20陕西2019ICPC省赛训练
E题
二分答案
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"
const int N=1e9+7;
string s;int n,k;
bool check(int l)
{
int ans=0;
for(int i=0;i<n;)
{
if(s[i]=='1')
{
i+=l;
ans++;
}
else
i++;
}
if(ans>k)//不行
{
return false;
}
else
return true;
}
void solve()
{
cin>>n>>k;
cin>>s;
int l=1;int r=n;
while(l<r)
{
int mid=l+r>>1;
if(check(mid)) r=mid;
else
l=mid+1;
}
cout<<l<<endl;
}
signed main()
{
IOS
int T=1;
cin>>T;
while(T--)
{
solve();
}
return 0;
}

浙公网安备 33010602011771号