题解 | #罪犯转移#
罪犯转移
https://www.nowcoder.com/practice/b7b1ad820f0a493aa128ed6c9e0af448
while True: try: key = list(map(int, input().split())) n, t, c = key[0], key[1], key[2] criminal = list(map(int, input().split())) left, right = 0, 0 res, crime = 0, 0 while right < n: crime += criminal[right] if right - left > c - 1: crime -= criminal[left] left += 1 if right - left == c - 1 and crime <= t: res += 1 right += 1 print(res) except: break
滑动窗口
首先明确两点:
所需窗口长度为c - 1
一开始先定义窗口的两侧都在起始位置index为0处
当窗口长度小于c - 1:
叠加犯罪值,右窗口右移
当窗口长度等于c - 1时:
判断当前犯罪值是否可以转移,可以则计数,右窗口右移
当窗口长度大于c - 1时:
先继续叠加犯罪值,然后将左侧窗口所指的犯罪值减掉,然后左右窗口同时右移