[USACO23DEC] Cowntact Tracing 2 B Python

先看题目:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值