#include <stdio.h>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
using namespace std;
const int inf = 1<<29;
int licz(int a, int b, int c, int d) {
long long r = a;
r *= b;
if (r > inf && c >= d) return inf;
r *= c;
r /= d;
if (r > inf) return inf;
return (int)r;
}
int znajdz(int a, int n, int L) {
if (a == 0) return 0;
int A = 1, B = n-L+1;
while (A < B) {
int x = (A+B) / 2;
int P = n-L-x;
if (licz(x, L, P, 1) + licz(x, x-1, L+P, 2) + licz(x, x-1, x-2, 6) >= a)
B = x;
else A = x+1;
}
return A;
}
bool ok(const vector<int>& a, int n, int s) {
int L = 0;
for (int i = 0; i < n; i++) {
L += znajdz(a[i], s, L);
if (L > s) return false;
}
return L <= s;
}
int main() {
int tt;
scanf("%d", &tt);
while(tt--) {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int A = 1, B = 200*n;
while (A < B) {
int s = (A+B)/2;
if (ok(a, n, s)) B = s;
else A = s+1;
}
printf("%d\n", A);
}
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 | #include <stdio.h> #include <vector> #include <algorithm> #include <utility> #include <map> using namespace std; const int inf = 1<<29; int licz(int a, int b, int c, int d) { long long r = a; r *= b; if (r > inf && c >= d) return inf; r *= c; r /= d; if (r > inf) return inf; return (int)r; } int znajdz(int a, int n, int L) { if (a == 0) return 0; int A = 1, B = n-L+1; while (A < B) { int x = (A+B) / 2; int P = n-L-x; if (licz(x, L, P, 1) + licz(x, x-1, L+P, 2) + licz(x, x-1, x-2, 6) >= a) B = x; else A = x+1; } return A; } bool ok(const vector<int>& a, int n, int s) { int L = 0; for (int i = 0; i < n; i++) { L += znajdz(a[i], s, L); if (L > s) return false; } return L <= s; } int main() { int tt; scanf("%d", &tt); while(tt--) { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int A = 1, B = 200*n; while (A < B) { int s = (A+B)/2; if (ok(a, n, s)) B = s; else A = s+1; } printf("%d\n", A); } return 0; } |
English