#include <bits/stdc++.h>
#define dbg(x) #x << " = " << x << " "
using namespace std;
using ll = long long;
constexpr int MAXN = 200'005;
bitset<2005> dp[2005];
ll ile(int l, int x, int r) {
return 1LL * l * x * r + 1LL * (x * (x - 1) / 2) * (l + r) + 1LL * x * (x - 1) * (x - 2) / 6;
}
vector<int> wywal_srodek(vector<int> &a) {
int n = a.size();
vector<int> res;
for (int i = 0; i < 1000; i++) res.push_back(a[i]);
for (int i = n - 1000; i < n; i++) res.push_back(a[i]);
return res;
}
int frajerstwo = 0;
bool check(vector<int> &a, int total) {
int n = a.size();
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
auto checkk = [&](int l) {
int L = l + (i - 1) + (i > 1000 ? frajerstwo : 0);
int R = total - l + (n - i) + (i <= 1000 ? frajerstwo : 0);
for (int x = 0; x <= l; x++, L--) {
if (!dp[i - 1][l - x]) continue;
ll mozna = ile(L, x + 1, R);
// cerr << mozna << "\t";
if (a[i - 1] <= mozna) { // spełnione dla dowolnego x >= 183.
return true;
}
}
return false;
};
int lo = 1, hi = total+1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (!checkk(mid)) lo = mid+1;
else hi = mid;
}
if (lo == total+1) return false;
dp[i].set();
dp[i] >>= lo;
dp[i] <<= lo;
}
return dp[n][total];
}
int solve(int n) {
vector<int> a;
for (int i = 1; i <= n; i++) {
int x = 1'000'000;
cin >> x;
if (x != 0) a.push_back(x);
}
n = a.size();
frajerstwo = 0;
if (n > 2000) {
a = wywal_srodek(a);
frajerstwo = n - 2000;
n = 2000;
}
int lo = 2, hi = 2000;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (!check(a, mid))
lo = mid + 1;
else
hi = mid;
}
return lo + n + frajerstwo;
}
int32_t main() {
ios_base::sync_with_stdio(0);
int tests = 100;
cin >> tests;
for (int tc = 1; tc <= tests; tc++) {
int n = tc * 10;
cin >> n;
cerr << dbg(n) << " ";
cout << solve(n) << "\n";
}
}
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 | #include <bits/stdc++.h> #define dbg(x) #x << " = " << x << " " using namespace std; using ll = long long; constexpr int MAXN = 200'005; bitset<2005> dp[2005]; ll ile(int l, int x, int r) { return 1LL * l * x * r + 1LL * (x * (x - 1) / 2) * (l + r) + 1LL * x * (x - 1) * (x - 2) / 6; } vector<int> wywal_srodek(vector<int> &a) { int n = a.size(); vector<int> res; for (int i = 0; i < 1000; i++) res.push_back(a[i]); for (int i = n - 1000; i < n; i++) res.push_back(a[i]); return res; } int frajerstwo = 0; bool check(vector<int> &a, int total) { int n = a.size(); dp[0][0] = 1; for (int i = 1; i <= n; i++) { auto checkk = [&](int l) { int L = l + (i - 1) + (i > 1000 ? frajerstwo : 0); int R = total - l + (n - i) + (i <= 1000 ? frajerstwo : 0); for (int x = 0; x <= l; x++, L--) { if (!dp[i - 1][l - x]) continue; ll mozna = ile(L, x + 1, R); // cerr << mozna << "\t"; if (a[i - 1] <= mozna) { // spełnione dla dowolnego x >= 183. return true; } } return false; }; int lo = 1, hi = total+1; while (lo < hi) { int mid = (lo + hi) / 2; if (!checkk(mid)) lo = mid+1; else hi = mid; } if (lo == total+1) return false; dp[i].set(); dp[i] >>= lo; dp[i] <<= lo; } return dp[n][total]; } int solve(int n) { vector<int> a; for (int i = 1; i <= n; i++) { int x = 1'000'000; cin >> x; if (x != 0) a.push_back(x); } n = a.size(); frajerstwo = 0; if (n > 2000) { a = wywal_srodek(a); frajerstwo = n - 2000; n = 2000; } int lo = 2, hi = 2000; while (lo < hi) { int mid = (lo + hi) / 2; if (!check(a, mid)) lo = mid + 1; else hi = mid; } return lo + n + frajerstwo; } int32_t main() { ios_base::sync_with_stdio(0); int tests = 100; cin >> tests; for (int tc = 1; tc <= tests; tc++) { int n = tc * 10; cin >> n; cerr << dbg(n) << " "; cout << solve(n) << "\n"; } } |
English