[CQOI 2010]扑克牌

时间:2022-02-09 16:35:22

Description

你有n种牌,第i种牌的数目为ci。另外有一种特殊的 牌:joker,它的数目是m。你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1套牌。比如,当n=3 时,一共有4种合法的套牌:{1,2,3}, {J,2,3}, {1,J,3}, {1,2,J}。 给出n, m和ci,你的任务是组成尽量多的套牌。每张牌最多只能用在一副套牌里(可以有牌不使用)。

Input

第一行包含两个整数n, m,即牌的种数和joker的个数。第二行包含n个整数ci,即每种牌的张数。

Output

输出仅一个整数,即最多组成的套牌数目。

Sample Input

3 4
1 2 3

Sample Output

3

HINT

样例解释
输入数据表明:一共有1个1,2个2,3个3,4个joker。最多可以组成三副套牌:{1,J,3}, {J,2,3}, {J,2,3},joker还剩一个,其余牌全部用完。

数据范围
50%的数据满足:2 < = n < = 5, 0 < = m < = 10^ 6, 0< = ci < = 200
100%的数据满足:2 < = n < = 50, 0 < = m, ci < = 500,000,000。

题解

看错两次题...

二分一下几副牌,然后要加的 $joker$ 数量 $\sum_{i = 1} ^n max(mid−c[i], 0)$

由抽屉原理, $joker$ 不能超过 $mid$ 张,所以加的 $joker$ 最多 $min(m,mid)$ 个。判断一下

 //It is made by Awson on 2017.10.9
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
using namespace std; LL n, m;
LL c[]; bool judge(LL mid) {
LL tol = ;
for (LL i = ; i <= n; i++)
if (c[i] < mid) tol += mid-c[i];
return tol <= min(m, mid);
}
void work() {
scanf("%lld%lld", &n, &m);
for (LL i = ; i <= n; i++)
scanf("%lld", &c[i]);
LL L = , R = 2e10, ans = ;
while (L <= R) {
LL mid = (L+R)>>;
if (judge(mid)) ans = mid, L = mid+;
else R = mid-;
}
printf("%lld\n", ans);
}
int main() {
work();
return ;
}