python二分只过了第一个样例,其他全都WA,求助!!!
查看原帖
python二分只过了第一个样例,其他全都WA,求助!!!
1207110
amm_rshcbqs楼主2024/3/13 18:07
def getLength(trees, high):
    summ = 0
    for i in trees:
        if high >= i:
            continue
        else:
            summ += (i - high)
    return summ


def find(trees, m):
    max_h = max(trees)
    min_h = 0
    while min_h < max_h:
        mid_h = (min_h + max_h) // 2
        length = getLength(trees, mid_h)
        if length <= m:
            max_h = mid_h
        elif length > m:
            min_h = mid_h + 1
    return max_h


if __name__ == '__main__':
    n, m = map(int, input().split())
    trees = [int(x) for x in input().split()]
    print(find(trees, m))
2024/3/13 18:07
加载中...