样例输入
4 5
1 2 3 4
5 5 5 5
样例输出
3
样例说明 这 5 张空白牌中, 拿 2 张写 1 , 拿 1 张写 2 , 这样每种牌的牌数就变为了 3,3,3,4, 可以凑出 3 套牌, 剩下 2 张空白牌不能再帮助小明凑出一套。
很好的一个思路 用优先队列每次取队头元素对m进行消耗,然后再插回堆中,当m消耗完之后队头的元素first值就是结果了,如果遇到消除途中second==0 意思也就是没有继续写的额度了,就break掉(这也是为什么在O(m+n)的复杂度下不会T的原因)。真妙蛙!
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+7;
typedef long long ll;
int a[N],b[N];
typedef pair<int,int> pr;
int main()
{
// 请在此输入您的代码
priority_queue<pr,vector<pr>,greater<pr> >q;
ll n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>b[i];
for(int i=1;i<=n;i++) {
q.push({
a[i],b[i]});
}
while(m){
auto it=q.top();q.pop();
int x=it.first,y=it.second;
if(y==0) break;
x++,y--,m--;
q.push({
x,y});
}
cout<<q.top().first;
return 0;
}