NC19916 [CQOI2010]扑克牌

题目地址:

https://ac.nowcoder.com/acm/problem/19916

基本思路:

考虑二分答案,二分能组成多少套牌,每次时,对于每种牌,我们算出不够的部分,这部分我们要用来替代,但是由于最多只能替代一张,所以最多也不会超过,那么算出多出的部分的总数和比较就能判断是否合法,并且我们容易发现答案有单调关系,因此二分成立。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int __int128
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int maxn = 1010;
int n,m,a[maxn];
bool check(int mid){
  int sum = 0;
  rep(i,1,n){
    if(a[i] < mid) sum += (mid - a[i]);
  }
  return sum <= min(m,mid);
}
signed main() {
  IO;
  n = read(),m = read();
  rep(i,1,n) a[i] = read();
  int l = 0,r = INF,ans = 0;
  while (l <= r){
    int mid = (l + r) >> 1;
    if(check(mid)) ans = mid,l = mid + 1;
    else r = mid - 1;
  }
  print(ans);
  puts("");
  return 0;
}