代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# @project : 《Atcoder》
# @Author : created by bensonrachel on 2021/8/18
# @File : TPM(step2)B. Segment with Big Sum.py
def TPM():
l = 0
sum = 0
res = n + 1
for r in range(n):
sum += rate[r]
while (sum >= s):
res = min(res, r - l + 1)
sum -= rate[l]
l += 1
return res
def TPM_on_theory():#这种写法才可以用来改写做D题
l = 0
sum = 0
res = 1000000
for r in range(n):
sum += rate[r]
while(sum-rate[l] >= s):
sum -= rate[l]
l += 1
if(sum>=s):
res = min(res,r-l+1)
"""
枚举r,维护最大的l,满足sum[l,r]>=s,
然后用r-l+1更新答案即可.
显然l的移动是向右单调的.
"""
if __name__ == "__main__":
n, s = map(int, input().split())
rate = [int(i) for i in input().split()]
x = TPM()
if (x == n + 1):
x = -1
print(x)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。