题解 P9975
HYdroKomide · · 题解
题意:
初始有一些区域为
思路:
所有连通块中的
但
最后,设初始时间
细节:左右靠边的连通块需要单独计算
程序如下:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<cstring>
using namespace std;
const int N=3e5+5;
int n,m,stb,edb,a[N],cnt,ans;
char str[N];
int main(){
scanf("%d%s",&n,str+1);
int st=1,ed=n;
while(str[st]=='1')st++,stb++;
while(str[ed]=='1')ed--,edb++;
for(int i=st;i<=ed;i++){
if(str[i]=='1'){
cnt++;
while(str[i]=='1')i++,a[cnt]++;
}
}
sort(a+1,a+cnt+1);
int tm=2e9;
if(a[1]!=0)tm=min(tm,(a[1]+1)/2);
if(stb!=0)tm=min(stb,tm);//要先确认左右不是0再取min
if(edb!=0)tm=min(edb,tm);
for(int i=1;i<=cnt;i++)ans+=ceil((double)(a[i])/(tm*2-1.));
ans+=ceil((double)stb/(tm*2-1));
if(st<=ed)ans+=ceil((double)edb/(tm*2-1));
printf("%d\n",ans);
return 0;
}