fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  5.  
  6. #define int long long
  7. #define pb push_back
  8. #define ff first
  9. #define ss second
  10. #define all(x) (x).begin(), (x).end()
  11. #define rall(x) (x).rbegin(), (x).rend()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14. #define yes cout << "yes\n"
  15. #define no cout << "no\n"
  16.  
  17. #define rep(i,a,b) for(int i=a;i<b;++i)
  18. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  19. #define each(x, a) for (auto& x : a)
  20.  
  21. const int INF = 1e18;
  22. const int MOD = 1e9+7;
  23. const int N = 2e5 + 5;
  24.  
  25. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  26. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  27.  
  28. int power(int a, int b, int m = MOD) {
  29. int res = 1;
  30. while (b > 0) {
  31. if (b & 1) res = res * a % m;
  32. a = a * a % m;
  33. b >>= 1;
  34. }
  35. return res;
  36. }
  37.  
  38. int modinv(int a, int m = MOD) {
  39. return power(a, m - 2, m);
  40. }
  41.  
  42. // Check function: can we generate the multiset 'positive_b' using max length 'x',
  43. // with a total of 'n' operations?
  44. bool check(int x, int n, const vector<int>& positive_b) {
  45. if (x == 0) return false;
  46. int total_required_ops = 0;
  47.  
  48. // R(x) = Sum of ceil(b_i / x) for all positive elements b_i
  49. each(val, positive_b) {
  50. // ceil(val / x) = (val + x - 1) / x
  51. total_required_ops += (val + x - 1) / x;
  52. }
  53.  
  54. // The required number of segments must be less than or equal to the total available operations (n).
  55. return total_required_ops <= n;
  56. }
  57.  
  58. void solve() {
  59. int n;
  60. cin >> n;
  61. vector<int> b(n);
  62. vector<int> positive_b;
  63. int sum_b = 0;
  64.  
  65. rep(i, 0, n) {
  66. cin >> b[i];
  67. sum_b += b[i];
  68. if (b[i] > 0) {
  69. positive_b.pb(b[i]);
  70. }
  71. }
  72.  
  73. // Special Case: If Sum(b_i) == n, then all n operations MUST have length 1.
  74. // Length (l, r) must be 1. Max length x=1.
  75. if (sum_b == n) {
  76. cout << 1 << endl;
  77. return;
  78. }
  79.  
  80. // General Case (Binary Search): Find the maximum length x such that the density constraints are met.
  81. int low = 1;
  82. int high = n;
  83. int ans = 0;
  84.  
  85. while (low <= high) {
  86. int mid = low + (high - low) / 2;
  87. if (check(mid, n, positive_b)) {
  88. ans = mid;
  89. low = mid + 1; // Try for a larger length
  90. } else {
  91. high = mid - 1; // Must use a smaller length
  92. }
  93. }
  94.  
  95. cout << ans << endl;
  96. }
  97.  
  98. int32_t main() {
  99. fast_io;
  100.  
  101. int t;
  102. cin >> t;
  103. while (t--) {
  104. solve();
  105. }
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0.01s 5308KB
stdin
3
5
0 5 1 0 1
3
3 2 1
5
1 1 1 1 1
stdout
5
0
1