题目大意: 有 n n n 种牌,每种牌有 a i a_i ai 张,以及有 m m m 张 j o k e r joker joker,现在问最多能凑几副牌,一张牌不能在多副牌中。一副牌由 n n n 张不同的牌组成。
题解
考虑二分答案,当答案为 k k k 时,对于第 i i i 种牌,假如 a i < k a_i<k ai<k,那么就需要 k − a i k-a_i k−ai 张 j o k e r joker joker,否则不需要 j o k e r joker joker。
于是可以统计出需要的 j o k e r joker joker 总数 s s s,假如 s > k s>k s>k,那么说明不存在方案能凑出 k k k 副牌,因为总有一副牌中用了至少 2 2 2 张 j o k e r joker joker,反之,假如 s ≤ k s\leq k s≤k,那么一定存在方案使得不存在两张 j o k e r joker joker 在同一副牌中。
然后再判一下 s s s 和 m m m 的大小关系即可。
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,a[60],l=0,r=1000000000,ans=0;
bool check(int x)
{
int tot=0;
for(int i=1;i<=n;i++)
{
tot+=max(0,x-a[i]);
if(tot>x||tot>m)return false;
}
return true;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
#define mid (l+r>>1)
while(l<=r)if(check(mid))ans=mid,l=mid+1;else r=mid-1;
printf("%d",ans);
}