学习笔记 | 最小堆、“种草”


学习笔记 | 最小堆、“种草”_第1张图片



def HeadAdjust(nums, k, n):
    nums[0] = nums[k]
    i = 2*k
    while i <= n:
        if i < n and nums[i] > nums[i+1]:
            i += 1
        if nums[0] <= nums[i]:
            break
        else:
            nums[k] = nums[i]
            k = i
        i = i*2
    nums[k] = nums[0]


def minHead(nums, n, x):
    for i in range(n//2, -1, -1):
        HeadAdjust(nums, i, n)


if __name__ == '__main__':
    nums = input().split()
    nums = [int(it) for it in nums]
    n = nums[0]
    m = nums[1]
    x = nums[2]

    xc = input().split()
    xc = [int(it) for it in xc]
    xc.insert(0, 0)

    minHead(xc, n, x)
    for _ in range(m):
        xc[1] += x
        HeadAdjust(xc, 1, n)

    print(xc[1])

你可能感兴趣的:(#,数据结构,刷题,面试)