题解 | 小红的口罩

小红的口罩

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

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define all(a) a.begin(), a.end()
using vi = vector<int>;

void solve() {
    int n, k;cin >> n >> k;
    vi a(n);
    for (int i = 0;i < n;i++) {
        cin >> a[i];
    }
    int sum = 0;
    priority_queue<int,vi,greater<int>>pq(all(a));
    int ans = 0;
    while (1) {
        int t = pq.top();
        pq.pop();
        pq.push(t * 2);
        if (sum + t > k) {
            break;
        }
        sum += t;
        ans++;
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; i++) {
        //cout << "----Test " << i << "----" << endl;
        solve();
    }
    return 0;
}

优先队列最小堆模拟

全部评论

相关推荐

04-08 16:35
门头沟学院 Java
站队站对牛:实在是恶心的公司
点赞 评论 收藏
分享
再懒也要睡懒觉:大学4年玩的挺爽的哈😅
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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