小根堆,模拟

小红的口罩

https://www.nowcoder.com/practice/fde642522d664b49a10fe9de51686026

思路:我们每次要选择最小的不舒适度,那就可以把数组a建小根堆,然后不断模拟即可

代码:

import sys
from heapq import heapify, heappop, heappush

input = lambda: sys.stdin.readline().strip()

import math
inf = 10 ** 18

def I():
    return input()

def II():
    return int(input())

def MII():
    return map(int, input().split())

def GMI():
    return map(lambda x: int(x) - 1, input().split())

def LI():
    return input().split()

def LII():
    return list(map(int, input().split()))

def LFI():
    return list(map(float, input().split()))

fmax = lambda x, y: x if x > y else y
fmin = lambda x, y: x if x < y else y
isqrt = lambda x: int(math.sqrt(x))

'''

'''

def solve():
    n, k = MII()
    a = LII()
    heapify(a)
    ans = 0
    while k > 0:
        mn = heappop(a)
        k -= mn
        if k >= 0:
            ans += 1
        heappush(a, 2 * mn)
    print(ans)

t = 1
# t = II()
for _ in range(t):
    solve()
#每日一题挑战#
全部评论

相关推荐

2025-12-01 15:50
内蒙古工业大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务