80pts,选官求条
查看原帖
80pts,选官求条
995215
sunhaozhe111022楼主2025/1/16 10:02
#include<bits/stdc++.h>
using namespace std;
long long n,m,a[1000005];
string s;
bool check(long long x)
{
	long long s=0,ze=0,on=0,d;
	for(int i=1;i<=n;i++)
	{
		if(a[i]) on++,d=1;
		else ze++,d=0;
		if(ze*on>x)
		{
			s++;
			if(d==1) on=1,ze=0;
			else on=0,ze=1;
		}
	}
	if(s+1>m) return true;
	return false;
}
int main()
{
	cin>>n>>m>>s;
	for(int i=1;i<=n;i++)
	{
		a[i]=s[i-1]-'0';
	}
	long long l=0,r=1e18,mid;
	while(l+1<r)
	{
		mid=(l+r)/2;
		if(check(mid)) l=mid;
		else r=mid;
	}
	cout<<r;
	return 0;
}
2025/1/16 10:02
加载中...