先看题目:
Farmer John has N cows in a line (1≤N≤3⋅105). Unfortunately, there is a sickness spreading throughout.
Initially, some cows start off infected. Every night, an infected cow spreads the sickness to the cows on their left and right (if they exist). Once a cow is infected, she stays infected.
After some amount of nights, Farmer John realizes that the issue has gotten out of control, so he tests his cows to determine who has the sickness. Find the minimum number of cows that could have started with the sickness.
啥?你看不懂英语?嘿嘿,我也看不懂,下面为翻译:
Farmer John 有 N 头奶牛排成一列(1≤N≤3⋅105)。不幸的是,有一种疾病正在传播。
最初,有一些奶牛被感染。每到夜晚,被感染的奶牛会将疾病传播给它左右两边的奶牛(如果这些奶牛存在的话)。一旦奶牛被感染,她就会持续处于感染状态。
经过一些晚上,Farmer John 意识到情况已经失控,因此他对奶牛进行了检测以确定哪些奶牛感染了疾病。请找出最少有多少头奶牛最初可能感染了这种疾病。
接下来,我们看看输入输出:
输入:
5 11111
输出:
1
接下来代码环节:
import: import math 输入: n=int(input()) c_l='0'+input()+'0' 变量: lj1=0 dylist=[9999999999999999999] ylist=[] for循环: for i in range(1,n+1): 有几个被感染的牛连在一起: if c_l[i-1]=='0' and c_l[i]=="1": bjt=i if c_l[i]=='1' and c_l[i+1]=='0': ylist.append(i-bjt+1) if bjt==1 or i==n: day= i-bjt else: day=(i-bjt)//2 dylist.append(day) 最少有多少牛: minday=min(dylist) cownum=0 for i in ylist: cownum+=math.ceil(i/(1+2*minday)) 输出: print(cownum)