std::sort(a + 1, a + 1 + n, std::greater<int>()); double sum = 0, ans = 0; rep(i, 1, n) { sum += a[i]; int q = n - i; if (m >= q) ans = std::max(ans, (sum + m - q) / i) }
现在我们加上 k 这个条件,我们只需改动一行代码(因为 k 的约束导致加上 m−q 的目标不一定可以实现
1
ans = std::max(ans, (sum + std::min((m - q), i * k)) / i);
#include<algorithm> #include<iomanip> #include<iostream> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, l, r) for (int i = (l); i >= (r); --i) using std::cerr; using std::cin; using std::cout; using std::endl; using std::make_pair; using std::pair; typedef pair<int, int> pii; typedeflonglong ll; typedefunsignedint ui;
ll a[100100];
intmain(){ std::ios::sync_with_stdio(false); cout.tie(0); ll n, k, m; cin >> n >> k >> m; double l, r; rep(i, 1, n) cin >> a[i]; std::sort(a + 1, a + 1 + n, std::greater<int>()); double sum = 0, ans = 0; rep(i, 1, n) { sum += a[i]; if (m >= (n - i)) ans = std::max(ans, (sum + std::min((m - (n - i)), i * k)) / i); } cout.setf(std::ios::fixed); cout << std::setprecision(14) << ans; return0; }