[CQOI 2010]扑克牌

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)$ 个。判断一下

 1 //It is made by Awson on 2017.10.9
 2 #include <set>
 3 #include <map>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <cmath>
 7 #include <stack>
 8 #include <queue>
 9 #include <vector>
10 #include <string>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 #define LL long long
17 #define Min(a, b) ((a) < (b) ? (a) : (b))
18 #define Max(a, b) ((a) > (b) ? (a) : (b))
19 #define sqr(x) ((x)*(x))
20 using namespace std;
21 
22 LL n, m;
23 LL c[55];
24 
25 bool judge(LL mid) {
26   LL tol = 0;
27   for (LL i = 1; i <= n;  i++)
28     if (c[i] < mid) tol += mid-c[i];
29   return tol <= min(m, mid);
30 }
31 void work() {
32   scanf("%lld%lld", &n, &m);
33   for (LL i = 1; i <= n; i++)
34     scanf("%lld", &c[i]);
35   LL L = 0, R = 2e10, ans = 0;
36   while (L <= R) {
37     LL mid = (L+R)>>1;
38     if (judge(mid)) ans = mid, L = mid+1;
39     else R = mid-1;
40   }
41   printf("%lld
", ans);
42 }
43 int main() {
44   work();
45   return 0;
46 }
原文地址:https://www.cnblogs.com/NaVi-Awson/p/7641892.html