题解 | 小红的口罩

小红的口罩

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,k;

int main()
{
    cin>>n>>k;
    int sum = 0;
    priority_queue<ll,vector<ll>,greater<ll>>pq;
    for(int i=1;i<=n;i++)
    {
        ll x;
        cin>>x;
        pq.push(x);
    }
    int ans = 0;
    while(sum<=k)
    {
        ans++;
        ll x = pq.top();
        pq.pop();
        sum+=x;
        x *= 2;
        pq.push(x);
    }
    ans--;
    cout<<ans<<'\n';

    return 0;
}

写个小根堆贪心即可,因为每次放入是×2的结果,所以时间复杂度是不会超的

全部评论

相关推荐

axiom15:校友,我感觉你这个简历去华子暑期实习随便去了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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