参考代码:
/* 排序 + 每次取小 #include <iostream> #include <algorithm> using namespace std; const int maxn = 100 + 5, INF = 0x3f3f3f3f; int n, k, value[maxn]; int main() { int Max = 0, L = 0; cin >> n >> k; int temp = k; for(int i = 0; i < n; i ++) { cin >> value[i]; Max = max(Max, value[i]); } sort(value, value + n); while(k > 0) { k -= (Max - value[L ++]); if(L == n) break; } if(k > 0) { cout << Max + (k / n) + (k % n == 0 ? 0 : 1) << ' '; } else { cout << Max << ' '; } cout << Max + temp << endl; return 0; } */ // /* 排序 + 二分 #include <iostream> #include <algorithm> #define mid ((l + r) / 2) using namespace std; const int maxn = 100 + 5; int n, k, value[maxn]; bool check(int a) { int sum = 0; for(int i = 0; i < n; i ++) { sum += a - value[i];//统计如果每个凳子坐a个人能够坐的人数的总数 } return sum >= k; } int main() { cin >> n >> k; int Max = 0, ans; for(int i = 0; i < n; i ++) { cin >> value[i]; Max = max(Max, value[i]); } sort(value, value + n); int l = Max, r = Max + k; while(l <= r) { if(check(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } cout << ans << ' ' << Max + k << endl; return 0; } // */