Card【Blue Bridge Cup National Competition】

Insert image description here

样例输入
4 5
1 2 3 4
5 5 5 5

样例输出
3

The example shows that among these 5 blank cards, take 2 and write 1, take 1 and write 2. In this way, the number of each card becomes 3, 3, 3, 4. You can make up 3 sets, leaving 2 A blank card can no longer help Xiao Ming make a set.

Insert image description here

A very good idea is to use the priority queue to take the head element of the queue each time to consume m, and then insert it back into the heap. When m is consumed, the first value of the element at the head of the queue is the result. If second==0 is encountered during elimination, This means that if there is no more room to continue writing, it will break (this is why there is no T under the complexity of O(m+n)). What a Bulbasaur!

#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;
}

Guess you like

Origin blog.csdn.net/weixin_51461002/article/details/131134472