#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
using namespace std;
using ll = long long;
ll choose_limit = 200; // change to 200
vector<ll> choose3(choose_limit + 1);
vector<ll> choose2(choose_limit + 1);
void gen_choose() {
choose3[0] = 0;
choose3[1] = 0;
choose3[2] = 0;
choose2[0] = 0;
choose2[1] = 0;
for (ll i = 2; i <= choose_limit; i++) {
choose2[i] = choose2[i - 1] + i - 1;
}
for (ll i = 3; i <= choose_limit; i++) {
choose3[i] = choose3[i - 1] + choose2[i - 1];
}
}
ll get_lower_bound(ll x) {
ll y = static_cast<ll>(cbrt(6.0 * x));
while ((y * (y - 1) * (y - 2)) / 6 < x) {
y++;
}
return y;
}
ll get_produced_games(ll x, ll t, ll u) {
// maximum_used_choose = max(maximum_used_choose, x);
ll term1 = choose3[x];
ll term2 = choose2[x] * (t - x);
ll term3 = x * u * (t - u - x);
return (term1 + term2 + term3);
}
bool check_constraint(ll x, ll t, ll u, ll c) {
return get_produced_games(x, t, u) >= c;
}
int main() {
gen_choose();
int tests;
cin >> tests;
while (tests--) {
ll n;
// n = 2;
n = 10000;
cin >> n;
vector<ll> c(n);
vector<ll> p(n);
for (ll i = 0; i < n; i++) {
// c[i] = 1000000;
cin >> c[i];
// c[0] = 4;
// c[1] = 4;
// c[2] = 4;
// c[3] = 3;
// c[4] = 0;
// c[5] = 60;
// c[6] = 70;
// c[0] = 57;
// c[1] = 20;
// c[2] = 30;
// c[3] = 40;
// c[4] = 50;
// c[5] = 60;
// c[6] = 70;
}
ll min_games_from_constraints = 0;
for (ll i = 0; i < n; i++) {
min_games_from_constraints += c[i];
}
ll lower_bound = get_lower_bound(min_games_from_constraints);
ll sum = 0;
vector<ll> g(n);
ll u = 0;
ll t;
ll left = lower_bound;
ll right = 250000;
ll min_t = LONG_LONG_MAX;
while (left <= right) {
t = (left + right) / 2;
bool found_x = false;
u = 0;
// cout << "L: " << left << ", R: " << right << endl;
// cout << "Trying t: " << t << endl;
for (ll i = 0; i < n; i++) {
found_x = false;
// cout << "Analyzing house: " << i << endl;
// cout << "Need games: " << c[i] << endl;
ll x = 0;
for (x; x <= t - u; x++) { // potential binary search here as well
// cout << "Trying x: " << x << endl;
if (check_constraint(x, t, u, c[i])) {
// cout << "Works x: " << x << endl;
found_x = true;
break;
}
else {
// cout << "Does not work x: " << x << endl;
}
}
if (!found_x) { // not enough players, try different t
// cout << "Not enough players, change t = " << t << endl;
break;
}
if (i == n - 1) { // use all people at the last house
x = t - u;
}
// cout << "Setting p[" << i << "] to " << x << endl;
p[i] = x;
u += x;
}
if (found_x) { // t works
min_t = min(min_t, t);
right = t - 1;
}
else { // t doesn't work
left = t + 1;
}
}
// min_t!
// assuming we always find a solution
// u = 0;
// for (ll i = 0; i < n; i++) {
// cout << p[i] << " " << get_produced_games(p[i], min_t, u) << endl;
// u += p[i];
// }
cout << min_t << endl;
// cout << "Min games required: " << min_games_from_constraints << endl;
// cout << "Lower bound: " << lower_bound << ", as (" << lower_bound << " 3) = " << choose3[min(choose_limit, lower_bound)] << endl;
// cout << "Actual players: " << min_t << ", allows (" << min_t << " 3) = " << choose3[min(choose_limit, min_t)] << endl;
// cout << "maximum_used_choose: " << maximum_used_choose << endl;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #include <iostream> #include <vector> #include <cmath> #include <climits> using namespace std; using ll = long long; ll choose_limit = 200; // change to 200 vector<ll> choose3(choose_limit + 1); vector<ll> choose2(choose_limit + 1); void gen_choose() { choose3[0] = 0; choose3[1] = 0; choose3[2] = 0; choose2[0] = 0; choose2[1] = 0; for (ll i = 2; i <= choose_limit; i++) { choose2[i] = choose2[i - 1] + i - 1; } for (ll i = 3; i <= choose_limit; i++) { choose3[i] = choose3[i - 1] + choose2[i - 1]; } } ll get_lower_bound(ll x) { ll y = static_cast<ll>(cbrt(6.0 * x)); while ((y * (y - 1) * (y - 2)) / 6 < x) { y++; } return y; } ll get_produced_games(ll x, ll t, ll u) { // maximum_used_choose = max(maximum_used_choose, x); ll term1 = choose3[x]; ll term2 = choose2[x] * (t - x); ll term3 = x * u * (t - u - x); return (term1 + term2 + term3); } bool check_constraint(ll x, ll t, ll u, ll c) { return get_produced_games(x, t, u) >= c; } int main() { gen_choose(); int tests; cin >> tests; while (tests--) { ll n; // n = 2; n = 10000; cin >> n; vector<ll> c(n); vector<ll> p(n); for (ll i = 0; i < n; i++) { // c[i] = 1000000; cin >> c[i]; // c[0] = 4; // c[1] = 4; // c[2] = 4; // c[3] = 3; // c[4] = 0; // c[5] = 60; // c[6] = 70; // c[0] = 57; // c[1] = 20; // c[2] = 30; // c[3] = 40; // c[4] = 50; // c[5] = 60; // c[6] = 70; } ll min_games_from_constraints = 0; for (ll i = 0; i < n; i++) { min_games_from_constraints += c[i]; } ll lower_bound = get_lower_bound(min_games_from_constraints); ll sum = 0; vector<ll> g(n); ll u = 0; ll t; ll left = lower_bound; ll right = 250000; ll min_t = LONG_LONG_MAX; while (left <= right) { t = (left + right) / 2; bool found_x = false; u = 0; // cout << "L: " << left << ", R: " << right << endl; // cout << "Trying t: " << t << endl; for (ll i = 0; i < n; i++) { found_x = false; // cout << "Analyzing house: " << i << endl; // cout << "Need games: " << c[i] << endl; ll x = 0; for (x; x <= t - u; x++) { // potential binary search here as well // cout << "Trying x: " << x << endl; if (check_constraint(x, t, u, c[i])) { // cout << "Works x: " << x << endl; found_x = true; break; } else { // cout << "Does not work x: " << x << endl; } } if (!found_x) { // not enough players, try different t // cout << "Not enough players, change t = " << t << endl; break; } if (i == n - 1) { // use all people at the last house x = t - u; } // cout << "Setting p[" << i << "] to " << x << endl; p[i] = x; u += x; } if (found_x) { // t works min_t = min(min_t, t); right = t - 1; } else { // t doesn't work left = t + 1; } } // min_t! // assuming we always find a solution // u = 0; // for (ll i = 0; i < n; i++) { // cout << p[i] << " " << get_produced_games(p[i], min_t, u) << endl; // u += p[i]; // } cout << min_t << endl; // cout << "Min games required: " << min_games_from_constraints << endl; // cout << "Lower bound: " << lower_bound << ", as (" << lower_bound << " 3) = " << choose3[min(choose_limit, lower_bound)] << endl; // cout << "Actual players: " << min_t << ", allows (" << min_t << " 3) = " << choose3[min(choose_limit, min_t)] << endl; // cout << "maximum_used_choose: " << maximum_used_choose << endl; } return 0; } |
English