#include <iostream>
#include <vector>
void go() {
int32_t n;
std::cin >> n;
std::vector<int32_t> a;
a.reserve(n);
for (int32_t i = 0; i < n; ++i) {
int32_t a1;
std::cin >> a1;
if (a1 > 0) {
a.push_back(a1);
}
}
int32_t min = a.size();
int32_t max = -1;
next:
if (min == max) {
std::cout << min + 2 << "\n";
return;
}
int64_t res = max == -1 ? min * 2 : (min + max) / 2;
int64_t i = 0;
int32_t lives = res - a.size();
for (int64_t val: a) {
lives += 1;
while (val > 0) {
if (lives == 0) {
min = res + 1;
goto next;
}
val -= (i + 1) * (res - i);
lives -= 1;
i += 1;
}
}
max = res;
goto next;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int32_t t;
std::cin >> t;
for (int32_t i = 0; i < t; ++i) {
go();
}
}
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 | #include <iostream> #include <vector> void go() { int32_t n; std::cin >> n; std::vector<int32_t> a; a.reserve(n); for (int32_t i = 0; i < n; ++i) { int32_t a1; std::cin >> a1; if (a1 > 0) { a.push_back(a1); } } int32_t min = a.size(); int32_t max = -1; next: if (min == max) { std::cout << min + 2 << "\n"; return; } int64_t res = max == -1 ? min * 2 : (min + max) / 2; int64_t i = 0; int32_t lives = res - a.size(); for (int64_t val: a) { lives += 1; while (val > 0) { if (lives == 0) { min = res + 1; goto next; } val -= (i + 1) * (res - i); lives -= 1; i += 1; } } max = res; goto next; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int32_t t; std::cin >> t; for (int32_t i = 0; i < t; ++i) { go(); } } |
English