本题为hard版本,和easy版本的唯一区别是有可能为负数! 小红拿到了一个数组,她想知道,有多少非空区间满足区间所有元素之和不小于?
区块链毕设网qklbishe.com为您提供问题的解答
本题为hard版本,和easy版本的唯一区别是
有可能为负数!
小红拿到了一个数组,她想知道,有多少非空区间满足区间所有元素之和不小于
?
”’
n, k = map(int, input().split())
a = list(map(int, input().split()))
count = 0
for i in range(len(a)):
for j in range(i,len(a)):
sub_list = a[i:j+1]
result = sum(sub_list)
if result >=k:
count += 1
print(count)
”’
import bisect
n, k = map(int, input().split())
a = list(map(int, input().split()))
prefix_sums = [0]
current_sum = 0
count = 0
for num in a:
current_sum += num
target = current_sum – k
# 找到有多少个前缀和小于等于target
cnt = bisect.bisect_right(prefix_sums, target)
count += cnt
# 将当前前缀和插入并保持有序
bisect.insort(prefix_sums, current_sum)
print(count)
18:52
以上就是关于问题本题为hard版本,和easy版本的唯一区别是有可能为负数!
小红拿到了一个数组,她想知道,有多少非空区间满足区间所有元素之和不小于?的答案
欢迎关注区块链毕设网-
web3一级市场套利打新赚钱空投教程
区块链NFT链游项目方科学家脚本开发培训