思路:二分答案 。check():检查组p套卡是否成立,即检查r卡是否足够组成p套卡。
(易错点:check的思路,开long long)
#include <bits/stdc++.h>
using namespace std;
long long int n, m;
long long int c[60];
bool check(long long int p) // p套卡
{long long int temp = 0; // 组p套卡需要多少张r卡for (long long int i = 1; i <= n; i++){if (p > c[i]){temp += p - c[i];}}if (temp <= m && temp <= p) // 一组最多有1张r卡{return true;}return false;
}
int main()
{cin >> n >> m;long long int maxn = 0;for (long long int i = 1; i <= n; i++){cin >> c[i];maxn = max(maxn, c[i]);}long long int ans = 0;// 二分答案long long int l = 0, r = maxn + 2 * m;while (l <= r){long long int mid = l + (r - l) / 2;if (check(mid)){l = mid + 1;ans = mid;}else{r = mid - 1;}}cout << ans;
}