原题链接:https://www.luogu.com.cn/problem/P1873
目录
1. 题目描述
2. 思路分析
3. 代码实现
1. 题目描述
2. 思路分析
二分答案。
3. 代码实现
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 10;
int a[N], n, m, highest;
ll sum;
bool check(int x)
{
sum = 0;
for (int i = 1; i <= n; i++)
{
sum += max(0, a[i] - x);
}
if (sum >= m) return true;
else return false;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
highest = max(highest, a[i]);
}
int l = 0, r = highest;
while (l + 1 < r)
{
int mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
cout << l << endl;
return 0;
}