P3718 WA求调,玄2关
  • 板块灌水区
  • 楼主PDAST
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/4/13 19:35
  • 上次更新2024/4/13 21:23:23
查看原帖
P3718 WA求调,玄2关
982629
PDAST楼主2024/4/13 19:35

WA了两个点,求大佬调,玄关

#include<bits/stdc++.h>
using namespace std;
int a[100001],n,k;
bool check(int len){
	int cnt=0,tmp=1,pre=a[1];
	for(int i=2;i<=n;i++){
		if(a[i]==pre){
			tmp++;
		}else{
			tmp=1;
			pre=!pre;
		}
		if(tmp>len){
			cnt++;
			tmp=1;
			pre=!pre;
		}
//		cout<<pre<<" ";
	}
//	cout<<endl;
	return cnt<=k;
}
int main(){
	cin>>n>>k;
	string st;
	cin>>st;
	for(int i=1;i<=n;i++){
		a[i]=(st[i-1]=='N'?1:0);
	}
	int l=1,r=n;
	while(l<r){
		int m=(l+r)>>1;
		if(check(m))r=m;
		else l=m+1;
//		cout<<l<<" "<<r<<" "<<m<<" "<<check(m)<<"\n";
	}
	cout<<r;
	return 0;
}

2024/4/13 19:35
加载中...