题解 | 小红的口罩
小红的口罩
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的结果,所以时间复杂度是不会超的
查看2道真题和解析